Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: When does Git perform garbage collection?

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?

like image 844
Wazery Avatar asked Jan 30 '15 23:01

Wazery


People also ask

When would a git commit get garbage collected?

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.

How would you force git to trigger garbage collection?

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.

When should you not run git gc '?

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.

Does git gc remove Stash?

git stash clear It will remove all the stashed changes.


1 Answers

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 using git repack -d -l. Setting the value of gc.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 of git repack. Setting gc.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
like image 179
torek Avatar answered Oct 21 '22 17:10

torek