如何真正删除 git 分支(即删除其所有对象/提交)?

发布于 2024-09-02 19:42:49 字数 484 浏览 1 评论 0 原文

我有一个 git 树,

                 A---B---C topic
                /
           D---E---F---G master     <--

我想删除主题及其上的所有对象。

我记下主题的 SHA ID,然后键入:

git branch -D topic
git gc                                   #  <-- I also tried prune here...
git checkout -b temp <SHA1 ID of topic>

在最后一个命令之后,我预计会收到错误(类似于“不存在的对象 ID...”或类似的内容)。但是没有错误并且 gitk 显示与上面相同的树结构?

我错过了什么 - 我认为 gc/prune 应该删除所有无法访问的对象?

I have a git tree like

                 A---B---C topic
                /
           D---E---F---G master     <--

I would like to remove topic and all objects on it.

I note the SHA ID of topic, then type:

git branch -D topic
git gc                                   #  <-- I also tried prune here...
git checkout -b temp <SHA1 ID of topic>

After the last command I expect to get an error (something like "Non-existent object ID..." or somth. like that). However there is no error and gitk shows the same tree structure as above??

What am I missing - I thought gc/prune are supposed to delete all unreachable objects?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掀纱窥君容 2024-09-09 19:42:49

仅 gc prune 通常不足以删除存储库中的额外对象。如果提交仍然在引用日志中被引用,它不会认为这些对象不可访问,因此适合修剪。

这对我有用:

git reflog expire --expire=now --all
git gc --aggressive --prune=now
git repack -a -d -l

这将对您的存储库历史记录进行一些更改,并且如果其他人依赖于您删除的分支,则很可能会带来困难。

您可能需要重新克隆存储库才能真正看到其大小的差异。

Just the gc prune is often not enough to get rid of the extra objects in the repo. If commits are still referenced in the reflog, it won't consider those objects unreachable and hence ripe for pruning.

Here's what worked for me:

git reflog expire --expire=now --all
git gc --aggressive --prune=now
git repack -a -d -l

This will make some changes to your repo's history, and may well present difficulties if others are depending on the branches you blow away.

You may have to re-clone your repository to actually see a difference in its size.

就是爱搞怪 2024-09-09 19:42:49

2010 年 5 月注释:正如提到的作者:Jakub,如果您的分支被合并,主题仍然可以访问。

在这里,我们假设没有合并。
然后,正如ProGit 书中提到的 以及SO问题

git gc --prune=now

应该足够了(你应该直接调用 git prune )。您可以使用 git count-objects -v 来控制。
2012 年 4 月编辑:评论中的 maxschlepzig 确认可能需要额外的步骤,详细信息请参见 Duke答案(但没有 <代码>git repack)。
因此,不要使用 git gc --prune now :

git reflog expire --expire=now --all
git gc --aggressive --prune=now

Note May 2010: As mentioned by Jakub, if your branch was merged, topic would still be reachable.

Here, let's suppose there was no merge.
Then, as mentioned in the ProGit book and detailed in this SO question:

git gc --prune=now

should be enough (you should call directly git prune). You can control that with a git count-objects -v.
Edit April 2012: maxschlepzig in the comments confirms that extra steps might be required, as detailed in Duke's answer (but without the git repack).
So instead of a git gc --prune now:

git reflog expire --expire=now --all
git gc --aggressive --prune=now
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文