您可以使用 Git 在一个命令中删除多个分支吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3670355/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:04:35  来源:igfitidea点击:

Can you delete multiple branches in one command with Git?

git

提问by Doug

I'd like to clean up my local repository, which has a ton of old branches: for example 3.2, 3.2.1, 3.2.2, etc.

我想清理我的本地库,其中有一吨老枝:例如3.23.2.13.2.2等。

I was hoping for a sneaky way to remove a lot of them at once. Since they mostly follow a dot release convention, I thought maybe there was a shortcut to say:

我希望有一种偷偷摸摸的方式一次删除很多。由于他们大多遵循点发布约定,我想也许有一个捷径可以说:

git branch -D 3.2.*

and kill all 3.2.x branches.

并杀死所有 3.2.x 分支。

I tried that command and it, of course, didn't work.

我试过那个命令,当然没有用。

回答by slebetman

Not with that syntax. But you can do it like this:

不是那种语法。但是你可以这样做:

git branch -D 3.2 3.2.1 3.2.2

Basically, git branch will delete multiple branch for you with a single invocation. Unfortunately it doesn't do branch name completion. Although, in bash, you can do:

基本上, git branch 将通过一次调用为您删除多个分支。不幸的是,它不做分支名称补全。虽然,在 bash 中,您可以执行以下操作:

git branch -D `git branch | grep -E '^3\.2\..*'`

回答by Carl Norum

Well, in the worst case, you could use:

好吧,在最坏的情况下,您可以使用:

git branch | grep '3\.2' | xargs git branch -d

回答by gawi

git branch  | cut -c3- | egrep "^3.2" | xargs git branch -D
  ^                ^                ^         ^ 
  |                |                |         |--- create arguments
  |                |                |              from standard input
  |                |                |
  |                |                |---your regexp 
  |                |
  |                |--- skip asterisk 
  |--- list all 
       local
       branches

EDIT:

编辑:

A safer version (suggested by Jakub Nar?bski and Jefromi), as git branch output is not meant to be used in scripting:

一个更安全的版本(由 Jakub Nar?bski 和 Jefromi 建议),因为 git 分支输出不打算在脚本中使用:

git for-each-ref --format="%(refname:short)" refs/heads/3.2\* | xargs git branch -D

... or the xargs-free:

... 或 xargs-free:

git branch -D `git for-each-ref --format="%(refname:short)" refs/heads/3.2\*`

回答by 50shadesofbae

You can use git branch --list to list the eligible branches, and use git branch -D/-d to remove the eligible branches.

您可以使用 git branch --list 列出符合条件的分支,并使用 git branch -D/-d 删除符合条件的分支。

One liner example:

一个班轮示例:

git branch -d `git branch --list '3.2.*'`

回答by K J Gor

Recently, I was looking for solution of same problem, finally i found one answer and it is working very well:

最近,我正在寻找相同问题的解决方案,最后我找到了一个答案,并且运行良好:

  1. Open the terminal, or equivalent.
  2. Type in git branch | grep " pattern " for a preview of the branches that will be deleted.
  3. Type in git branch | grep " pattern " | xargs git branch -D
  1. 打开终端,或等效的。
  2. 输入 git 分支 | grep " pattern " 用于预览将被删除的分支。
  3. 输入 git 分支 | grep "模式" | xargs git 分支 -D

This solution is awesome and if you want full explanation of each command and how it is working, its given here.

这个解决方案很棒,如果您想完整解释每个命令及其工作方式,请在此处提供

回答by Jakub Nar?bski

Use

git for-each-ref --format='%(refname:short)' 'refs/heads/3.2.*' |
   xargs git branch -D

回答by Adi

Maybe You will find this useful:

也许你会发现这很有用:

If You want to remove all branches that are not for example 'master', 'foo' and 'bar'

如果您想删除所有不是“master”、“foo”和“bar”的分支

git branch -D `git branch | grep -vE 'master|foo|bar'`

grep -v 'something' is a matcher with inversion.

grep -v 'something' 是一个具有反转功能的匹配器。

回答by Efpophis

If you're not limited to using the git command prompt, then you can always run git guiand use the Branch->Delete menu item. Select all the branches you want to delete and let it rip.

如果您不仅限于使用 git 命令提示符,那么您始终可以运行git gui并使用 Branch->Delete 菜单项。选择要删除的所有分支,然后将其撕掉。

回答by Imtiaz Shakil Siddique

Powershell Solution:

Powershell 解决方案:

If you're running windows, you can use PowerShell to remove multiple branches at once...

如果您正在运行 Windows,则可以使用 PowerShell 一次删除多个分支...

git branch -D ((git branch | Select-String -Pattern '^\s*3\.2\..*') | foreach{$_.ToString().Trim()})
//this will remove all branches starting with 3.2.

git branch -D ((git branch | Select-String -Pattern 'feature-*') | foreach{$_.ToString().Trim()})
// this will delete all branches that contains the word "feature-" as a substring.

You can also fine tune how the pattern matching should work using Powershell's Select-String command. Take a look at powershell docs.

您还可以使用 Powershell 的 Select-String 命令微调模式匹配的工作方式。查看powershell 文档

回答by Alan Tweedie

I put my initials and a dash (at-) as the first three characters of the branch name for this exact reason:

正是出于这个原因,我将我的首字母和破折号 (at-) 作为分支名称的前三个字符:

git branch -D `git branch --list 'at-*'`