我如何删除已合并的所有Git分支? [英] How can I delete all Git branches which have been merged?
问题描述
我有很多Git分支。如何删除已经合并的分行?有没有一种简单的方法可以将它们全部删除,而不是逐个删除它们?
I have many Git branches. How do I delete branches which have already been merged? Is there an easy way to delete them all instead of deleting them one by one?
推荐答案
更新:
如果您的工作流程将这些分支作为可能的祖先,您可以添加其他分支以排除主控和开发。通常我从sprint-start标签分支出来,而master,dev和qa不是祖先。
You can add other branches to exclude like master and dev if your workflow has those as a possible ancestor. Usually I branch off of a "sprint-start" tag and master, dev and qa are not ancestors.
删除已经合并到当前选中的所有本地分支out分支:
To delete all local branches that are already merged into the currently checked out branch:
git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d
您可以看到master和dev在它们是祖先的情况下被排除。
You can see that master and dev are excluded in case they are an ancestor.
您可以删除合并的本地分支:
You can delete a merged local branch with:
git branch -d branchname
如果未合并,请使用:
If it's not merged, use:
git branch -D branchname
在旧版Git中从远程删除它:
To delete it from the remote in old versions of Git use:
git push origin :branchname
在更新版本的Git中使用:
In more recent versions of Git use:
git push --delete origin branchname
删除从远程的分支,你可以修剪,以摆脱远程跟踪分支:
Once you delete the branch from the remote, you can prune to get rid of remote tracking branches with:
git remote prune origin
或修剪个别远程追踪分支,如其他答案所示:
or prune individual remote tracking branches, as the other answer suggests, with:
git branch -dr branchname
希望这有助于您。
这篇关于我如何删除已合并的所有Git分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!