Delete git branches whose name matches a certain pattern

GitGithub

Git Problem Overview


How can I delete 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 wan't to delete the branches origin_alpha, origin_beta and origin_gamma.

Git Solutions


Solution 1 - Git

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

Solution 2 - Git

Another way could be this:

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

to me looks more intuitive because grep is something I use daily.

You could also make an alias of it (or also of any solution suggested here), check for example here how to pass arguments to an alias: http://www.cyberciti.biz/faq/linux-unix-pass-argument-to-alias-command/

PS in your specific case, yourSearchPattern could be origin:

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

PPS as next step, would be also nice to make the deleting process more verbose, for example would be nice that you have to confirm the delete for each branch. But I think that overcomes the question...

Solution 3 - Git

git branch -D $(git branch --list 'regex_here')
Example: \
git branch -D $(git branch --list 'aputhen/*')
Deletes all branches with name starting with aputhen/.

Solution 4 - Git

Late to the party but another way of doing this is

git branch -d `git branch | grep substring`

and for current question

git branch -d `git branch | grep origin`

This will delete all branches whose names contain origin.

Solution 5 - Git

Adding the script I use in PowerShell (Windows)

Foreach ($branch in (git branch --list | findstr user)) { git branch -D $branch.trim() }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNirmalya GhoshView Question on Stackoverflow
Solution 1 - Githek2mglView Answer on Stackoverflow
Solution 2 - GitlzzlucaView Answer on Stackoverflow
Solution 3 - GitAustin p.bView Answer on Stackoverflow
Solution 4 - GitPranav GuptaView Answer on Stackoverflow
Solution 5 - GitAditya TView Answer on Stackoverflow