Remove git branches whose name matches a specific pattern

How to remove branches in git starting with the letter "o"?

Suppose I have a list of branches like the following:

origin_alpha
origin_beta
origin_gamma
alpha
beta
gamma

I do not want to delete the branches origin_alpha , origin_beta strong> and origin_gamma .

+21
source share
3 answers

Update: The option -rfor xargsis a complement to GNU. If you do not use xargsfindutils from GNU, this may not work. You can omit it, but this will result in an error if the input for xargs is empty.


You can use git branch --list <pattern>and direct its output to xargs git branch -d:

git branch --list 'o*' | xargs -r git branch -d

, . , o, git branch --list 'o*' :

* origin_master
origin_test
o_what_a_branch

* .

, , xargs * git branch delete.

, , , :

git branch --list 'o*' | sed 's/^* //' | xargs -r git branch -d
+41

:

git branch -d $(git branch | grep yourSearchPattern)

, grep - , .

( , ), , , : http://www.cyberciti.biz/faq/linux-unix-pass-argument-to-alias-command/

PS yourSearchPattern :

git branch -d $(git branch | grep origin)

PPS , , , , . , ...

+13

Late to the party, but another way to do it is

git branch -d 'git branch | grep substring'

and for the current question

git branch -d 'git branch | grep origin'

This will delete all branches whose names contain the origin.

0
source

Source: https://habr.com/ru/post/1615253/


All Articles