I was wondering: when does Git perform its garbage collection? I know that in the past one had to invoke git gc
to manually start the garbage collection, but now it is done automatically, when?
Also, is there a need to invoke it manually in the latest Git versions?
Garbage collection in interpreted languages is used to recover memory that has become inaccessible to the executing program. Git repositories accumulate various types of garbage. One type of Git garbage is orphaned or inaccessible commits.
The easiest option would be to use a scheduled task in windows or a cron job in Unix to run git gc periodically. This way you don't even need to think about it.
See gc. auto below for how to disable this behavior. Running git gc manually should only be needed when adding objects to a repository without regularly running such porcelain commands, to do a one-off repository optimization, or e.g. to clean up a suboptimal mass-import.
git stash clear It will remove all the stashed changes.
Much of the answer is in the git gc
documentation:
--auto
With this option, git gc checks whether any housekeeping is required; if not, it exits without performing any work. Some git commands run
git gc --auto
after performing operations that could create many loose objects.Housekeeping is required if there are too many loose objects or too many packs in the repository. If the number of loose objects exceeds the value of the
gc.auto
configuration variable, then all loose objects are combined into a single pack usinggit repack -d -l
. Setting the value ofgc.auto
to 0 disables automatic packing of loose objects.If the number of packs exceeds the value of
gc.autopacklimit
, then existing packs (except those marked with a.keep
file) are consolidated into a single pack by using the-A
option ofgit repack
. Settinggc.autopacklimit
to 0 disables automatic consolidation of packs.
The only thing missing here is an explanation of which "some" commands might run git gc --auto
, and when. This list is subject to change, but looking at current git source, these stand out:
git fetch
git merge
git receive-pack
git am
git rebase
(this is from git grep -e --auto -- '*.c' '*.sh'
and eyeball-excluding all the t/
tests scripts and other obvious false hits). If you want something more in depth, the source to git is on github.com
...
Note: with Git 2.17 (Q2 2018), you need to consider also:
git commit
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