Cleaning Up Unused Objects in Git Repository
After months of development, the .git folder was taking up way more space than it should. My Git repository has grown huge and I need to clean up all the garbage. Been experimenting with various git cleanup commands and found a combination that really works.
The Solution
The first solution that git offers is git gc. It is used for garbage collection and repository maintenance. It cleans up and optimizes the repository by compressing file revisions, removing unreachable objects, and reclaiming disk space to improve performance.
But regular git gc wasn't aggressive enough to clean up everything.
So I came up with a script that does a really thorough cleanup. Warning: this will remove ALL unreferenced objects, so make sure you don't need any of them!
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 can help too:
git -c gc.reflogExpire=0 -c gc.reflogExpireUnreachable=0 \
-c gc.rerereresolved=0 -c gc.rerereunresolved=0 \
-c gc.pruneExpire=now gc --aggressive
Additional Cleanup
Sometimes you need to clean up refs and logs first:
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
What This Does
gc.reflogExpire=0: Immediately expire reflog entriesgc.reflogExpireUnreachable=0: Don't keep unreachable reflog entriesgc.rerereresolved=0: Clean up rerere resolved conflictsgc.rerereunresolved=0: Clean up rerere unresolved conflictsgc.pruneExpire=now: Prune objects immediately
Results
In my case, this reduced my repository size from 150MB down to 12MB. Pretty significant savings!
Warning
This is pretty aggressive cleanup. Don't blame me if you lose something you wanted to keep. Make sure you have backups of anything important.
Git can be complicated when it comes to garbage collection, but this approach gets the job done.