#git fetch --all
Setup git hooks to automatically gitignore large files
In this assignment we’re dealing with large files that will end up in the ../data and ../output directories. To prevent those large files from clogging up our ability to ‘git push’, we can use built-in hooks to automatically ignore files larger than 100 MB (no matter the directory or file name!). Here are the steps to follow:
Create a new text file in the
.git/hooks/
directory of your repository calledpre-commit
Select the
More
tab with the gear icon under the RStudio Files navigator bar and select ‘show hidden files’ to see the .git folder.Add the following text to the
.git/hooks/pre-commit
file:
#!/bin/sh
# Maximum file size (in bytes)
max_file_size=104857600
# Find all files larger than max_file_size and add them to the .gitignore file
find . -type f -size +$max_file_size -exec echo "{}" >> .gitignore \;
This code sets the max_file_size variable to 100 MB and then uses the find command to locate all files in the repository that are larger than the specified max_file_size. The exec option of the find command appends the name of each file that matches the criteria to the .gitignore file.
or
#!/bin/bash
echo “automatically ignoring large files” find . -size 5M | sed ‘s|^./||g’ >> .gitignore cat .gitignore | sort | uniq > .gitignore
git diff –exit-code .gitignore exit_status=$? if [ $exit_status -eq 1 ] then set +e for i in cat .gitignore
do set +e git rm –cached $i done
git add .gitignore
git commit .gitignore --no-verify -m"ignoring large files"
echo "ignored new large files"
It is pretty brute force and the downside is that in case there were new large files added by the git hook, the origin commit fails because the state (hash) changed. So you need to execute another commit to actually commit what you have staged. Consider this as a feature telling you that new large files were detected ;-)
Save the pre-commit file and make it executable by running the following command in Terminal from your base git directory:
chmod +x .git/hooks/pre-commit
With these changes, whenever you run a git commit command, Git will first execute the pre-commit hook, which will automatically add any files larger than 100 MB to the .gitignore file. This will prevent Git from tracking these files in the repository going forward.
Manually execute code to find and add files >1G to .gitignore before committing
Use the following code as a bash terminal command to find and add files >1G from repo to .gitignore find . -size +1G | sed 's|^./||g' | cat >> .gitignore
from THIS Robert’s Lab Course Issue
Using git revert
:
- Open your terminal or command prompt.
- Navigate to the repository directory using the **`cd`** command.
- Execute the following command to revert the most recent commit:
`git revert HEAD`
- Git will create a new commit that undoes the changes introduced by the previous commit. A text editor will open for you to provide a commit message. Save and close the editor to complete the revert process.
Using git reset
- Open your terminal or command prompt.
- Navigate to the repository directory using the **`cd`** command.
- Execute the following command to reset the repository to the commit before the most recent one:
`git reset HEAD~1`
This command moves the branch pointer to the commit before the most recent one, effectively undoing the last commit.
- Note that the changes introduced in the undone commit will still be present in your working directory. You can then modify or discard them as needed.
Please exercise caution when using these commands, as they modify the Git history. If you have already pushed the commit you want to undo to a remote repository, it’s generally not recommended to modify the history, as it can cause conflicts for other team members. In that case, it may be better to consider reverting the changes in a new commit and pushing that instead.
Last Resort… Burn it down
In the event that you accidentally committed a big file (>100MB), you can reset to the last successful git master branch push
If you still want to continue, you can un-comment the code and follow this instruction:
First, update all
origin/<branch>
refs to latest:Backup your current branch (e.g.
master
):#git branch backup-branch
Jump to the latest commit on
origin/master
:#git reset --hard origin/main