How to remove unused objects from a git repository? How to remove unused objects from a git repository? git git

How to remove unused objects from a git repository?


I answered this elsewhere, and will copy here since I'm proud of it!

... and without further ado, may I present to you this useful script, git-gc-all, guaranteed to remove all your git garbage until they might come up with 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 "$@"

The --aggressive option might be helpful.

NOTE: this will remove ALL unreferenced thingies, so don't come crying to me if you decide later that you wanted to keep some of them!

You might also need to run something like these first, oh dear, git is complicated!!

git remote rm originrm -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

I put all this in a script, here:

http://sam.nipl.net/b/git-gc-all-ferocious


Your git reflog expire --all is incorrect. It removes reflog entries that are older than the expire time, which defaults to 90 days. Use git reflog expire --all --expire=now.

My answer to a similar question deals with the problem of really scrubbing unused objects from a repository.


1) Remove the file from the git repo (& not the filesystem) :

  • git rm --cached path/to/file

2) Shrink the repo using:

  • git gc,

  • or git gc --aggressive

  • or git prune

or a combination of the above as suggested in this question: Reduce git repository size