bash 删除所有本地 git 分支

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

Delete all local git branches

gitbashubuntuversion-controlgrep

提问by Louth

I follow a development process where I create a new local branch for every new feature or story card. When finished I merge the branch into master and then push.

我遵循一个开发过程,为每个新功能或故事卡创建一个新的本地分支。完成后,我将分支合并到 master 中,然后推送。

What tends to happen over time due to a combination of laziness or forgetfulness, is that I end up with a large list of local branches, some of which (such as spikes) may not have been merged.

由于懒惰或健忘的组合,随着时间的推移往往会发生什么,我最终会得到一大堆本地分支,其中一些(例如尖峰)可能没有合并。

I know how to list all my local branches and I know how to remove a single branch but I was wondering if there was a git command that allows me to delete all my local branches?

我知道如何列出所有本地分支,也知道如何删除单个分支,但我想知道是否有一个 git 命令可以让我删除所有本地分支?

Below is the output of the git branch --mergedcommand.

下面是git branch --merged命令的输出。

user@machine:~/projects/application[master]$ git branch --merged
  STORY-123-Short-Description
  STORY-456-Another-Description
  STORY-789-Blah-Blah
* master

All attempts to delete branches listed with grep -v \*(as per the answers below) result in errors:

所有尝试删除列出的分支grep -v \*(根据下面的答案)都会导致错误:

error: branch 'STORY-123-Short-Description' not found.
error: branch 'STORY-456-Another-Description' not found.
error: branch 'STORY-789-Blah-Blah' not found.

I'm using:
git 1.7.4.1
ubuntu 10.04
GNU bash, version 4.1.5(1)-release
GNU grep 2.5.4

我正在使用:
git 1.7.4.1
ubuntu 10.04
GNU bash,版本 4.1.5(1)-release
GNU grep 2.5.4

回答by GoZoner

The 'git branch -d' subcommand can delete more than one branch. So, simplifying @sblom's answer but adding a critical xargs:

'git branch -d' 子命令可以删除多个分支。因此,简化@sblom 的答案,但添加一个关键的 xargs:

git branch -D `git branch --merged | grep -v \* | xargs`

or, further simplified to:

或者,进一步简化为:

git branch --merged | grep -v \* | xargs git branch -D 

Importantly, as noted by @AndrewC, using git branchfor scripting is discouraged. To avoid it use something like:

重要的是,正如@AndrewC 所指出的,git branch不鼓励使用for 脚本。为了避免它使用类似的东西:

git for-each-ref --format '%(refname:short)' refs/heads | grep -v master | xargs git branch -D

Caution warranted on deletes!

删除时需谨慎!

$ mkdir br
$ cd br; git init
Initialized empty Git repository in /Users/ebg/test/br/.git/
$ touch README; git add README; git commit -m 'First commit'
[master (root-commit) 1d738b5] First commit
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README
$ git branch Story-123-a
$ git branch Story-123-b
$ git branch Story-123-c
$ git branch --merged
  Story-123-a
  Story-123-b
  Story-123-c
* master
$ git branch --merged | grep -v \* | xargs
Story-123-a Story-123-b Story-123-c
$ git branch --merged | grep -v \* | xargs git branch -D
Deleted branch Story-123-a (was 1d738b5).
Deleted branch Story-123-b (was 1d738b5).
Deleted branch Story-123-c (was 1d738b5).

回答by mBardos

I found a nicer way in a comment on this issue on github:

在 github 上对这个问题的评论中找到了一个更好的方法:

git branch --merged master --no-color | grep -v master | grep -v stable | xargs git branch -d

git branch --merged master --no-color | grep -v master | grep -v stable | xargs git branch -d

edit:added no-color option and excluding of stable branch (add other branches as needed in your case)

编辑:添加无颜色选项并排除稳定分支(根据您的情况添加其他分支)

回答by Andrew C

Parsing the output of git branchis not recommended, and not a good answer for future readers on Stack Overflow.

git branch不推荐解析 的输出,对于 Stack Overflow 的未来读者来说,这不是一个好的答案。

  1. git branchis what is known as a porcelain command. Porcelain commands are not designed to be machine parsed and the output may change between different versions of Git.
  2. There are user configuration options that change the output of git branchin a way that makes it difficult to parse (for instance, colorization). If a user has set color.branchthen you will get control codes in the output, this will lead to error: branch 'foo' not found.if you attempt to pipe it into another command. You can bypass this with the --no-colorflag to git branch, but who knows what other user configurations might break things.
  3. git branchmay do other things that are annoying to parse, like put an asterisk next to the currently checked out branch
  1. git branch就是所谓的瓷器命令。瓷器命令不是为机器解析而设计的,不同版本的 Git 之间的输出可能会有所不同。
  2. 有一些用户配置选项会以git branch难以解析的方式更改输出(例如,着色)。如果用户已设置,color.branch那么您将在输出中获得控制代码,error: branch 'foo' not found.如果您尝试将其通过管道传输到另一个命令中,则会导致此错误。您可以使用--no-colorto 标志绕过它git branch,但谁知道其他用户配置可能会破坏事情。
  3. git branch可能会做其他解析很烦人的事情,比如在当前签出的分支旁边放一个星号

The maintainer of git has this to sayabout scripting around git branchoutput

git维护者有话要说围绕git branch输出编写脚本

To find out what the current branch is, casual/careless users may have scripted around git branch, which is wrong. We actively discourage against use of any Porcelain command, including git branch, in scripts, because the output from the command is subject to change to help human consumption use case.

为了找出当前分支是什么,随意/粗心的用户可能已经围绕 git branch 编写了脚本,这是错误的。我们积极反对在脚本中使用任何 Porcelain 命令,包括 git branch,因为命令的输出可能会更改以帮助人类消费用例。

Answers that suggest manually editing files in the .gitdirectory (like .git/refs/heads) are similarly problematic (refs may be in .git/packed-refs instead, or Git may change their internal layout in the future).

建议手动编辑目录中的.git文件(如 .git/refs/heads)的答案同样存在问题(refs 可能在 .git/packed-refs 中,或者 Git 将来可能会更改其内部布局)。

Git provides the for-each-refcommand to retrieve a list of branches.

Git 提供了for-each-ref检索分支列表的命令。

Git 2.7.X will introduce the --mergedoption to so you could do something like the below to find and delete all branches merged into HEAD

Git 2.7.X 将引入--merged选项,以便您可以执行以下操作来查找和删除合并到的所有分支HEAD

for mergedBranch in $(git for-each-ref --format '%(refname:short)' --merged HEAD refs/heads/)
do
    git branch -d ${mergedBranch}
done

Git 2.6.X and older, you will need to list all local branches and then test them individually to see if they have been merged (which will be significantly slower and slightly more complicated).

Git 2.6.X 及更早版本,您需要列出所有本地分支,然后单独测试它们以查看它们是否已合并(这会显着变慢且稍微复杂一些)。

for branch in $(git for-each-ref --format '%(refname:short)' refs/heads/)
do
    git merge-base --is-ancestor ${branch} HEAD && git branch -d ${branch}
done

回答by geoom

The simpler way to delete all branches but keeping others like "develop" and "master" is the following:

删除所有分支但保留其他分支(如“develop”和“master”)的更简单方法如下:

git branch | grep -v "develop" | grep -v "master" | xargs git branch -D

very useful !

很有用 !

回答by sblom

To delete every branch except the one that you currently have checked out:

要删除除您当前已签出的分支之外的所有分支:

for b in `git branch --merged | grep -v \*`; do git branch -D $b; done

I would recommend changing git branch -D $bto an echo $bthe first few times to make sure that it deletes the branches that you intend.

我建议在前几次更改git branch -D $b为 aecho $b以确保它删除您想要的分支。

回答by Hunter

The below command will delete all the local branches except master branch.

下面的命令将删除除 master 分支之外的所有本地分支。

git branch | grep -v "master" | xargs git branch -D

The above command

上面的命令

  1. list all the branches
  2. From the list ignore the master branch and take the rest of the branches
  3. delete the branch
  1. 列出所有分支
  2. 从列表中忽略主分支并取其余分支
  3. 删除分支

回答by Adam Dymitruk

Just a note, I would upgrade to git 1.7.10. You may be getting answers here that won't work on your version. My guess is that you would have to prefix the branch name with refs/heads/.

请注意,我会升级到 git 1.7.10。您可能会在这里得到不适用于您的版本的答案。我的猜测是您必须在分支名称前加上refs/heads/.

CAUTION, proceed with the following only if you made a copy of your working folder and .git directory.

注意,仅当您复制了工作文件夹和 .git 目录时才继续以下操作。

I sometimes just go ahead and delete the branches I don't want straight from .git/refs/heads. All these branches are text files that contain the 40 character sha-1 of the commit they point to. You will have extraneous information in your .git/configif you had specific tracking set up for any of them. You can delete those entries manually as well.

有时我会直接删除我不想要的分支.git/refs/heads。所有这些分支都是包含它们指向的提交的 40 个字符的 sha-1 的文本文件。.git/config如果您为它们中的任何一个设置了特定的跟踪,您就会有无关的信息。您也可以手动删除这些条目。

回答by Pravin

Try the following shell command:

尝试以下 shell 命令:

git branch | grep -v "master" | xargs git branch -D

Explanation:

解释:

  • Get all branches via git branch | grep -v "master"command
  • Select every branch with xargscommand
  • Delete branch with xargs git branch -D
  • 通过git branch | grep -v "master"命令获取所有分支
  • xargs命令选择每个分支
  • 删除分支 xargs git branch -D

回答by Ashish Yadav

To delete all local branches in linux exceptthe one you are on

删除Linux中的所有本地分支,除了所在的分支

// hard delete

git branch -D $(git branch)

回答by culebrón

I found it easier to just use text editor and shell.

我发现只使用文本编辑器和 shell 更容易。

  1. Type git checkout <TAB>in shell. Will show all local branches.
  2. Copy them to a text editor, remove those you need to keep.
  3. Replace line breaks with spaces. (In SublimeText it's super easy.)
  4. Open shell, type git branch -D <PASTE THE BRANCHES NAMES HERE>.
  1. 键入git checkout <TAB>壳。将显示所有本地分支。
  2. 将它们复制到文本编辑器,删除您需要保留的那些。
  3. 用空格替换换行符。(在 SublimeText 中非常简单。)
  4. 打开外壳,输入git branch -D <PASTE THE BRANCHES NAMES HERE>.

That's it.

就是这样。