I have a GitHub repo that had two branches - master & release.
The release branch contained binary distribution files that were contributing to a very large repo
Sometimes, the reason that "gc" doesn't do much good is that there is an unfinished rebase or stash based on an old commit.
You can use git forget-blob
.
The usage is pretty simple git forget-blob file-to-forget
. You can get more info here
https://ownyourbits.com/2017/01/18/completely-remove-a-file-from-a-git-repository-with-git-forget-blob/
It will disappear from all the commits in your history, reflog, tags and so on
I run into the same problem every now and then, and everytime I have to come back to this post and others, that's why I automated the process.
Credits to contributors such as Sam Watkins
Before doing git filter-branch
and git gc
, you should review tags that are present in your repo. Any real system which has automatic tagging for things like continuous integration and deployments will make unwanted objects still referenced by these tags, hence gc
can't remove them and you will still keep wondering why the size of repo is still so big.
The best way to get rid of all un-wanted stuff is to run git-filter
& git gc
and then push master to a new bare repo. The new bare repo will have the cleaned up tree.
... and without further ado, may I present to you this useful command, "git-gc-all", guaranteed to remove all your git garbage until they might come up extra config variables:
git -c gc.reflogExpire=0 -c gc.reflogExpireUnreachable=0 -c gc.rerereresolved=0 -c gc.rerereunresolved=0 -c gc.pruneExpire=now gc
You might also need to run something like these first, oh dear, git is complicated!!
git remote rm origin
rm -rf .git/refs/original/ .git/refs/remotes/ .git/*_HEAD .git/logs/
git for-each-ref --format="%(refname)" refs/original/ | xargs -n1 --no-run-if-empty git update-ref -d
You might also need to remove some tags, thanks Zitrax:
git tag | xargs git tag -d
I put all this in a script: git-gc-all-ferocious.
To add another tip, don't forget to use git remote prune to delete the obsolete branches of your remotes before using git gc
you can see them with git branch -a
It's often useful when you fetch from github and forked repositories...
git gc --prune=now
, or low level git prune --expire now
.