使用git批量删除分支

要删除本地,首先要考虑以下三点

  • 列出所有本地分支
  • 搜索目标分支如:所有含有‘dev’的分支
  • 将搜索出的结果传给删除函数

所以我们可以得到:

    git br |grep 'dev' |xargs git br -d

 

本地新建了很多分支,比如

$ git branch
brabch
branch2
branch3
branch4
chucklu_zhCN
* master

 

其中以bra开头的分支都是临时性的分支,用完之后需要删除,使用命令逐个删除就太麻烦了

 

$ git branch |grep 'bran'
branch2
branch3
branch4

 

$ git branch |grep 'bran'|xargs git branch -d
Deleted branch branch2 (was a84d992).
Deleted branch branch3 (was 95a769c).
Deleted branch branch4 (was 9e7aecb).

 

$ git branch |grep 'bra'|xargs git branch -d
Deleted branch brabch (was e71cd6d).

 

 

参考资料:

Linux中的管道命令操作符 | http://www.cnblogs.com/chengmo/archive/2010/10/21/1856577.html

grep命令  http://www.cnblogs.com/peida/archive/2012/12/17/2821195.html

xargs命令 http://baike.baidu.com/link?url=9v3akR6obUlic6nkh5U4JBG4B7F3WsDxJOOUa_q3_b9ruseDhkcoIJ6rI5CzgP9h1kAXtVCM7g19VYM5_T6-cK

批量删除分支(包括本地和远端的)http://scriptogr.am/pison/post/git

 

 

Delete git branches whose name matches a certain pattern

 
 

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.


You can use git branch --list <pattern> and pipe it's output to 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:

* 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.

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

 

posted @ 2015-09-26 14:50  ChuckLu  阅读(2009)  评论(0编辑  收藏  举报