如何删除所有本地git分支?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (179)

下面是git branch --merged命令。

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

尝试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.
提问于
用户回答回答于

‘git分支-d’子命令可以删除多个分支。

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

或进一步简化为:

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

git branch可以使用如下的方法:

git for-each-ref --format '%(refname:short)' refs/heads | grep -v master | 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/refs/heads所有这些分支都是文本文件

扫码关注云+社区

领取腾讯云代金券