首页·终端命令

git gc

git

用法

git gc [--aggressive] [--prune=<date>]

把零散对象打包、丢掉无人引用的,从而缩小 .git;--prune=now 连 reflog 还拉着的提交一起丢掉,撤销 reset 的安全网也就没了。

常用选项

选项含义
--aggressiverepack harder; slow, occasionally worth it
--prune=<date>drop unreachable objects older than that
--prune=nowdrop them all, which also throws away reflog-only commits
--autodo nothing unless there is enough loose junk
--no-prunerepack but keep unreachable objects
-qno progress output

示例

git gc

packs loose objects and shrinks .git

git gc --auto

the cheap check git itself runs after some commands

git gc --prune=now --aggressive

smallest result, and no way back to lost commits

难的地方在于撤销有好几种:把分支的指向挪走,和补一个反向提交,不是一回事。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. git gc 是做什么的?

把零散对象打包、丢掉无人引用的,从而缩小 .git;--prune=now 连 reflog 还拉着的提交一起丢掉,撤销 reset 的安全网也就没了。

Q. 怎么写?

git gc [--aggressive] [--prune=<date>] —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 6 个,完整列表在 man git。这条命令属于git。

相关命令

man 手册: man git-gc