删除所有本地 git 分支 [英] Delete all local git branches

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

问题描述

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

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.

由于懒惰或健忘的组合,随着时间的推移往往会发生的事情是,我最终得到了大量本地分支,其中一些(例如尖峰)可能尚未合并.

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.

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

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 branch --merged 命令的输出.

Below is the output of the git branch --merged command.

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

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

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

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

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

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 branch -d' 子命令可以删除多个分支.因此,简化@sblom 的答案,但添加一个关键的 xargs:

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

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

或者,进一步简化为:

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

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

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

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

谨慎删除!

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

这篇关于删除所有本地 git 分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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