git - 从 git 存储库中删除未引用的提交

标签 git branch commit git-branch git-rebase

我有这样的 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/

相关文章:

svn - 我如何自动将提交部署到网站?

version-control - Mercurial 防止文件被更改

git - 当我推送到 BitBucket GIT 存储库时,如何自动将代码推送到 Azure 网站?

c# - Linux 上 Mono 中的 System.Speech

git - 如何在不从 git 历史记录中删除分支的情况下关闭它?

git - 如何删除所有远程 git 分支

branch - git-svn branch - 如何使分支与主干保持同步?

database - 什么是 "distributed transaction"?

windows - 有什么办法可以半自动提交吗?

git checkout -- * 并跳过未跟踪的文件