71

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.

Flip
  • 6,233
  • 7
  • 46
  • 75
Nirmalya Ghosh
  • 2,298
  • 3
  • 18
  • 33
  • Possible duplicate of [Can you delete multiple branches in one command with Git?](http://stackoverflow.com/questions/3670355/can-you-delete-multiple-branches-in-one-command-with-git) – cmbuckley Nov 10 '15 at 13:42
  • I'm not aware of any limitation preventing you from deleting those branches as you would any other branch: `git branch -d origin_alpha` – kvdv Nov 10 '15 at 13:42
  • 2
    @kvdv Thanks for your answer. Suppose, I've 50 branches starting with the letter 'o', then it isn't convenient to manually delete all those branches. Hence, the question. – Nirmalya Ghosh Nov 10 '15 at 13:46
  • @cmbuckley Thanks for your answer. I wanted to delete those branches only which starts with a definite letter. So, my question is different from the one that you mentioned. – Nirmalya Ghosh Nov 10 '15 at 13:47
  • @NirmalyaGhosh no, it's the same, but the match is `o*` instead of `3.2*`. See the answer http://stackoverflow.com/a/28614187/283078. – cmbuckley Nov 10 '15 at 13:49
  • It can be the same but the answer which @hek2mgl provided seemed easier to me. – Nirmalya Ghosh Nov 10 '15 at 13:54
  • Change the title to something like "delete git branches with names that math a pattern" or something more correct in english for future reference. – Jepessen Nov 10 '15 at 14:09

5 Answers5

119

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
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • This won't work if you're on one of those branches, as your output will contain the highlighting asterisk. See the answer http://stackoverflow.com/a/3670560/283078. – cmbuckley Nov 10 '15 at 13:51
  • Switched to master and applied this code. Solved my problem. Thanks. – Nirmalya Ghosh Nov 10 '15 at 13:51
  • 2
    @cmbuckley `git branch -d ` would also not work if you are on that branch. ;) That's why I've ignored that. – hek2mgl Nov 10 '15 at 13:53
  • This must be a windows thing, because while `git branch --list 'hotfix'` shows a list of branches for me, `git branch --list 'hotfix' | xargs git branch -d` gives me *fatal: branch name required* – PandaWood Jul 23 '18 at 00:25
  • What gives you `git branch --list 'hotfix'`? – hek2mgl Jul 23 '18 at 06:39
  • not sure why this answer is so popular, @Izzluca's answer is much more cleaner and comprehensible. – Eduard Jul 30 '18 at 12:54
  • @Eduard Define _much more cleaner_! Basically my answer is `git branch --list 'o*' | xargs -r git branch -d` - which is actually quite clean and short. The problem is with that `*`, that's why it needs the additional sed. The other answer would fail in that case. Having that, my best guess is that this answer is so popular because it works :) – hek2mgl Jul 30 '18 at 14:06
  • git branch --list *o* | xargs -r git branch -d works on windows – Feiyu Zhou Aug 01 '18 at 06:14
  • 12
    Just tried on a Mac: xargs doesn't accept `-r` (`illegal option -- r`) but it worked without it for the desired purpose. – Amy Pellegrini Aug 22 '18 at 10:55
  • Use linux! ;) ... Thanks for the note! – hek2mgl Aug 22 '18 at 11:24
  • 2
    FYI The single quotes kill it for me on Windows right now `git branch --list 'hotfix'` shows nothing but `git branch --list "hotfix"` does the trick – PandaWood Oct 06 '20 at 23:49
  • To use it as an alias (caution no warnings) , add in your ~/.gitconfig `brrm = "!f(){ git branch --list \"$1\" | xargs -r git branch -d; }; f" ` then use it with `git brrm feature*` – pdem Oct 14 '21 at 08:02
46

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

lzzluca
  • 706
  • 5
  • 14
  • I'm on windows right now, and though I have the full unix command set, like grep, your suggestion here eg `git branch -d $(git branch | grep hotfix)` fails with `error: branch '$(git' not found` – PandaWood Jul 23 '18 at 00:27
  • At the first sight: is `$()` is correctly recognised by Win? seems that git is trying to remove the branch `$(git` instead of replacing it with the command output – lzzluca Jul 24 '18 at 12:05
  • It would be great to see the elaboration on what the first line of code after "-d" does. – Eduard Jul 30 '18 at 12:10
16

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

eli
  • 8,571
  • 4
  • 30
  • 40
Austin p.b
  • 465
  • 7
  • 13
6

Adding the script I use in PowerShell (Windows)

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

Aditya T
  • 90
  • 1
  • 5
5

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.

Pranav Gupta
  • 741
  • 9
  • 10