I forgot to add certain files to my .gitignore when I began my project and consequently committed files I shouldn't have. I ignored the files afterwards as described here. The damage has already happened though since the files still exist in the history of my repository and now my repository is 10gb in size!
I have not pushed the files for the aforementioned reason, so rewriting history should be okay. In short, what I need to do is to rewrite history so that afterwards none of the files in the current .gitignore
exist in any commit in the repository.
Edit: There are lots of small files contributing to the large size, so the suggested duplicate about how to remove all files above a certain size threshold does not solve this problem.
Git can only ignore files that are untracked - files that haven't been committed to the repository, yet. That's why, when you create a new repository, you should also create a . gitignore file with all the file patterns you want to ignore.
If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a . gitignore rule for it. Using the --cached option with git rm means that the file will be deleted from your repository, but will remain in your working directory as an ignored file.
The easiest way to delete a file in your Git repository is to execute the “git rm” command and to specify the file to be deleted. Note that by using the “git rm” command, the file will also be deleted from the filesystem.
To reset the commit histories as original, you can use git reset --hard origin/branchname
.
To ignore files and remove them from history, you can follow below two aspects:
.gitignore
file (if you don’t have) by touch .gitignore
..gitignore
. The wildcard is allowed. Then commit the changes.git rm filename -r --cached git commit
git filter-branch --index-filter 'git rm --ignore-unmatch -r --cached filename' --prune-empty -f -- --all
To tidy your local repository (you can also skip this step):
rm -Rf .git/refs/original
rm -Rf .git/logs/
git gc
git prune --expire now
To push the rewrite history to remote:
git push -f --all
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With