如何清理提交树中未使用的侧枝?

eri*_*ork 7 git git-rebase

你如何清理你的提交树(不是真正的git分支)中未使用的侧枝?

示例(树,伪提交哈希,提交消息,可选[指针]):

*    0001 last commit [master] [origin/master] [HEAD]
| *  0002 old, unused merge
|/|
* |  0003 some remote commits
* |  0004 another commit from remote
| *  0005 old, unused commits
|/
*    0006 old tree
Run Code Online (Sandbox Code Playgroud)

路径0001 0003,0004,0006应该保持不变,但是提交0002和0005没有用,并且没有任何好处.你如何删除提交0002和0005?

eri*_*ork 11

tarsius 在回答另一个问题时写道:

git reflog expire --expire=now --all
git gc --prune=now
Run Code Online (Sandbox Code Playgroud)

它清除了reflog,然后清理了存储库.首先清理reflog并不总是有效,因为git-gc只要reflog没有到期(默认为90天),reflog所标记的意义​​提交就会保持活动状态.

在我这样做之后,所有悬挂的提交都真的消失了.所以应该确定一个人真的不再需要所有这些了.如果一个人真的想要保留一些悬空提交,可以:

git checkout <dangling_commit_id>
git branch <new_branch_name_of_your_choice>
Run Code Online (Sandbox Code Playgroud)

或使用git format-patch将整个提交存储在文本文件中.