如果你执行 git branch -d branchname
,如果它引用了你历史中较早的提交,它将删除分支名称,或者告诉你你需要使用 -D
否则。我经常创建后来推送到 master 的分支,因此可以按此标准删除。有没有一种简单的方法可以列出所有指向 master 早期提交的分支,即 git 不介意仅使用 -d
选项删除的分支?如果它同时适用于所有分支,而不仅仅是大师,则可获得加分。
我有 82 个本地分支机构,我知道现在可以安全地删除其中的大部分(如果不是大部分的话),但我不想花时间逐一尝试删除。
最佳答案
尝试:
$ git checkout master # or whatever branch you might compare against ...
$ git branch --no-merged
$ git branch --merged
With --merged, only branches merged into the named commit (i.e. the branches whose tip commits are reachable from the named commit) will be listed. With --no-merged only branches not merged into the named commit will be listed. If the argument is missing it defaults to HEAD (i.e. the tip of the current branch).
编辑:
要为每个分支显示此信息,您可以执行以下操作:
示例 repo :
o <--- experimental
|
o
|
o <--- next
|
o
|
o <--- master
|
o----o <--- broken
|
o
|
$ for branch in `git branch --no-color --verbose | \
sed -e 's/*//' | awk '{print $1}'`; \
do echo "[$branch]"; git checkout -q $branch; git branch --merged; done
[broken]
* broken
[master]
* master
[next]
master
* next
[experimental]
master
next
* experimental
关于git - 列出可以安全删除的 git 分支,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2353804/