Git:Git什么时候进行垃圾回收?

Git: When does Git perform garbage collection?

我想知道:Git 什么时候执行垃圾收集?我知道过去必须调用 git gc 来手动启动垃圾收集,但现在它是自动完成的,什么时候?

另外,在最新的Git版本中是否需要手动调用它?

大部分答案都在 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.

这里唯一缺少的是对哪些“某些”命令可能 运行 git gc --auto 以及何时执行的解释。此列表可能会更改,但查看当前 git 来源,这些突出显示:

git fetch
git merge
git receive-pack
git am
git rebase

(这来自 git grep -e --auto -- '*.c' '*.sh' 和眼球——排除了所有 t/ 测试脚本和其他明显的错误命中)。如果你想要更深入的东西,git 的来源在 github.com...

注意:with Git 2.17 (Q2 2018),您还需要考虑:

git commit