【问题标题】:Remove extraneous commits from a git repository从 git 存储库中删除无关的提交
【发布时间】:2019-10-10 13:47:46
【问题描述】:

我有一个存储库,其中包含来自不同项目的一堆提交,它们不在当前项目的任何分支上。它们只是出现在相互连接的历史记录中,但不连接到项目中的任何分支。

因此,如果我执行git branch --contains <commit hash>,它不会返回任何内容。

(我相信这些是在对远程 gitolite3 存储库进行一些更改并在其状态错误时进行提取时到达那里的)。

我想从提交数据库中删除它们,因为它们根本不属于这里。

我该怎么做?

我在git reflog expire --expire=now --expire-unreachable=now --all 之前和之后尝试过git prune,但它并没有删除它们。我也试过git gc,但也没有用。

这似乎与 Completely remove commit from git database 有关,但我在 reflog 或引用这些提交的分支中找不到任何内容,这会阻止 prune 删除它们。我讨厌只需要从数据库中删除对象,这似乎是一种糟糕的做事方式。

【问题讨论】:

    标签: git


    【解决方案1】:

    可能导致git gc 不删除提交的另一件事是是否有标签引用它或包含它的链。

    git tag --list # shows the tags
    git tag -d <extraneous tag> # removes any such tags
    git gc # then deletes the extraneous commits
    

    这是相关帖子Remove commits that are not connected to the branch建议的

    【讨论】:

    • 或者,与git tag --contains联系。如果您的 Git 不是太旧,git for-each-ref --contains &lt;commit&gt; 将列出分支、标签和任何其他引用。
    猜你喜欢
    • 2012-11-28
    • 2015-11-05
    • 2021-05-24
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    • 2020-07-16
    • 1970-01-01
    • 2011-09-21
    相关资源
    最近更新 更多