Git gc stands for garbage collection. There are multiple types of “garbage” including orphaned, unreachable and dangling objects. These Git objects will be part of another post in the future, so for now think of it as removing any unnecessary files and optimizing the repository’s disk size. Although git gc
its executed automatically there are some cases where you might consider triggering it manually.
Prerequisites
- Git
Solution
how often to execute git gc manually
By default git gc
runs automatically on every git pull, merge, commit, rebase, and in case there are too many loose objects. You can count the loose objects by running: git count-objects
.
However, there’s no solid answer yet, so you can definitely trigger the garbage collection manually whenever you feel like, and also depending on the current size and the “activity” level of the repo. For larger size repos from 1-5 GB, sure it makes sense, though the GitHub’s hard size limit is capped at 100GB.
My advice is to leave it as it is, forget about running git gc
unless it’s explicitly required.
git gc vs git prune
Git prune is a part of the git gc
command. Git prune takes care of every Git object that’s beyond reach by git gc
itself. To use it in combination with git prune
, run:
git gc --prune=now
other
git gc --auto
checks if garbage cleaning is necessary before doing the actual work.
git gc --aggressive
does a forced (-f
) and better thorough cleaning. Often used in combination with git prune: git gc --aggressive --prune=now
.
Conclusion
To find more neat Git commands and hacks, simply browse the Git category. Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.