我有这样的 git 提交历史:
U
/
A---B---C---D---E master
没有任何内容指向提交 U
,但我知道它的哈希值。如何从我的存储库中完全删除此提交,就好像它从未存在过一样?我是唯一使用此存储库的人。
我尝试使用 git rebase
,但这可以删除部分分支或移动提交,但似乎无法删除单个提交。
如果我这样做 git checkout <hash>
然后 git reset --hard HEAD~1
我再也看不到提交了。它真的完全消失了还是仍然 stash 在 repo 协议(protocol)中?
最佳答案
如所述here , 只需使用
git reflog expire --expire-unreachable=now --all
git gc --prune=now
git reflog expire --expire-unreachable=now --all
删除 reflog
中所有无法访问的提交的引用。
git gc --prune=now
删除提交本身。
注意:仅使用 git gc --prune=now
将不起作用,因为这些提交仍在 reflog 中引用。因此,必须清除 reflog。
关于git - 从 git 存储库中删除未引用的提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13812103/