列出可安全删除的git分支 [英] List git branches that can safely be deleted

查看:156
本文介绍了列出可安全删除的git分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你使用 git branch -d branchname ,它会删除分支名称,如果它引用了历史记录中的早期提交,或者告诉你需要使用 -D 否则。我经常创建一些分支,这些分支稍后会推送给主,因此可以通过此标准删除。有没有简单的方法来列出所有指向早期提交主分支的分支,也就是说,git不会介意只用 -d 选项删除的分支?奖金分数,如果它一次适用于所有分支,而不仅仅是主人。

我有82个本地分支机构,我知道如果不是大多数的分支机构现在可以安全地删除它们,我不想接受时间去审查每一个尝试去做。

解决方案

试试:

  $ git checkout master#或任何可能与之比较的分支... 
$ git branch --no-merged
$ git branch --merged

git分支文档


使用--merged时,只有分支合并到指定的提交中(也就是说,其顶端提交的分支可以从指定的提交)将被列出。使用--no-merged只分支未合并到指定的提交将被列出。如果参数缺失,则默认为HEAD(即当前分支的尖端)。


编辑:



为每个分支显示这一点,你可以做这样的事情:



示例repo: p>

  o <--- experimental 
|
o
|
o< --- next
|
o
|
o< --- master
|
o ---- o <---破碎
|
o
|


$用于`git分支的分支--no-color --verbose | \
sed -e's / * //'| awk'{print $ 1}'; \
做echo[$ branch]; git checkout -q $分支; git branch --merged;完成

[坏]
*破碎
[高手]
*主人
[下一个]
主人
*下一个
[实验]
主人
下一个
*实验


If you do git branch -d branchname, it will delete branch name if it references an earlier commit in your history, or tell you that you need to use -D otherwise. I often create branches that are later pushed to master, and can thus be deleted by this criterion. Is there an easy way to list all the branches that point to earlier commits of master, that is, branches that git will not mind deleting with just the -d option? Bonus points if it works for all branches at once, not just master.

I've got 82 local branches and I know that quite a few if not most of them can safely be deleted by now, but I don't want to take the time to go through each one to try to do it.

解决方案

Try:

$ git checkout master # or whatever branch you might compare against ...
$ git branch --no-merged
$ git branch --merged

From git branch documentation:

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).

EDIT:

to show this for every branch, you could do something like this:

example 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分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆