删除名称与特定模式匹配的 git 分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33631326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete git branches whose name matches a certain pattern
提问by Nirmalya Ghosh
How can I delete branches in git starting with the letter 'o'?
如何删除git中以字母“o”开头的分支?
Suppose, I have a list of branches like the following:
假设,我有一个如下所示的分支列表:
origin_alpha
origin_beta
origin_gamma
alpha
beta
gamma
I wan't to delete the branches origin_alpha, origin_betaand origin_gamma.
我不想删除分支origin_alpha、origin_beta和origin_gamma。
回答by hek2mgl
Update: The -r
option to xargs
is a GNU addon. Unless you use xargs
from GNU findutils it might not work. You can omit it but that leads to an error if the input piped to xargs is empty.
更新:-r
选项xargs
是 GNU 插件。除非您使用xargs
GNU findutils,否则它可能无法工作。您可以省略它,但如果通过管道传输到 xargs 的输入为空,则会导致错误。
You can use git branch --list <pattern>
and pipe it's output to xargs git branch -d
:
您可以使用git branch --list <pattern>
并将其输出通过管道传输到xargs git branch -d
:
git branch --list 'o*' | xargs -r git branch -d
Btw, there is a minor issue with the code above. If you've currently checked out one of the branches that begins with o
the output of git branch --list 'o*'
would look like this:
顺便说一句,上面的代码有一个小问题。如果您当前已签出以o
输出开头的分支之一,则git branch --list 'o*'
如下所示:
* origin_master
origin_test
o_what_a_branch
Note the asterisk *
in front of the current branch name.
注意*
当前分支名称前面的星号。
While you cannot delete the current branch anyway, it leads to the fact that xargs also passes *
to git branch delete
.
虽然无论如何您都无法删除当前分支,但它会导致 xargs 也传递*
给git branch delete
.
As I say it is just a cosmetic error, but if you want to avoid it use:
正如我所说,这只是一个表面错误,但如果你想避免它,请使用:
git branch --list 'o*' | sed 's/^* //' | xargs -r git branch -d
回答by lzzluca
Another way could be this:
另一种方式可能是这样的:
git branch -d $(git branch | grep yourSearchPattern)
to me looks more intuitive because grep is something I use daily.
对我来说看起来更直观,因为 grep 是我每天使用的东西。
You could also make an alias of it (or also of any solution suggested here), check for example here how to pass arguments to an alias: http://www.cyberciti.biz/faq/linux-unix-pass-argument-to-alias-command/
您还可以为其创建别名(或此处建议的任何解决方案),例如在此处检查如何将参数传递给别名:http: //www.cyberciti.biz/faq/linux-unix-pass-argument-别名命令/
PS in your specific case, yourSearchPattern could be origin:
PS 在您的特定情况下, yourSearchPattern 可能是原点:
git branch -d $(git branch | grep origin)
PPS as next step, would be also nice to make the deleting process more verbose, for example would be nice that you have to confirm the delete for each branch. But I think that overcomes the question...
PPS 作为下一步,也可以让删除过程更加冗长,例如,您必须确认每个分支的删除。但我认为这克服了这个问题......
回答by Pranav Gupta
Late to the party but another way of doing this is
聚会迟到,但另一种方式是
git branch -d `git branch | grep substring`
and for current question
对于当前问题
git branch -d `git branch | grep origin`
This will delete all branches whose names contain origin.
这将删除名称包含 origin 的所有分支。
回答by Aditya T
Adding the script I use in PowerShell (Windows)
添加我在 PowerShell (Windows) 中使用的脚本
Foreach ($branch in (git branch --list | findstr user)) { git branch -D $branch.trim() }
Foreach ($branch in (git branch --list | findstr user)) { git branch -D $branch.trim() }
回答by Austin p.b
git branch -D $(git branch --list 'regex_here' )
git branch -D $(git branch --list 'regex_here' )
eg :
git branch -D $(git branch --list 'aputhen/*' ) deletes all branches starting with aputhen/
.
例如: git branch -D $(git branch --list 'aputhen/*' ) 删除所有以aputhen/
.