使用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
批量删除分支(包括本地和远端的)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
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了