首頁·終端機命令

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