How to REALLY delete a git branch (i.e. remove all of its objects/commits)?

随声附和 提交于 2019-11-27 00:44:52

问题


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?


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/2882375/how-to-really-delete-a-git-branch-i-e-remove-all-of-its-objects-commits

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!