【问题标题】:Remove an unreferenced commit from git repository从 git 存储库中删除未引用的提交
【发布时间】:2012-11-28 12:35:33
【问题描述】:

我有这样的 git 提交历史:

          U
         / 
A---B---C---D---E master

没有任何东西指向提交U,但我知道它的哈希值。如何从我的存储库中完全删除此提交,就好像它从未存在过一样?我是唯一使用这个 repo 的人。

我尝试使用git rebase,但这可以删除部分分支或移动提交,但似乎无法删除单个提交。

如果我先 git checkout <hash> 然后 git reset --hard HEAD~1 我就看不到提交了。它实际上是完全消失了还是仍然隐藏在 repo 中?

【问题讨论】:

    标签: git branch commit git-branch git-rebase


    【解决方案1】:

    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。

    【讨论】:

      【解决方案2】:

      最终它将被 git 清理,但您可以查看git gc 以实际强制进行垃圾收集。

      clean 命令不同,不会为您处理这个问题。

      【讨论】:

      • 如果我克隆了整个仓库怎么办?未引用的垃圾对象也会被克隆吗?
      • @Jawap:不,它们不会被克隆
      猜你喜欢
      • 2019-10-10
      • 2015-11-05
      • 2014-03-24
      • 2017-07-27
      • 2010-12-26
      • 1970-01-01
      • 1970-01-01
      • 2020-07-16
      • 2021-05-24
      相关资源
      最近更新 更多