tags:

views:

66

answers:

4

I'd like to clean up my local repository which has a ton of old branches, let's say 3.2, 3.2.1, 3.2.2, etc.

I was hoping for a sneaky way to remove a lot of them at once and since they mostly follow a dot release convention, I thought maybe there was a shortcut to say:

git branch -D 3.2.*

and kill all 3.2.x branches

I tried that command and it of course didn't work... :(

A: 

As far as I know git-branch does not allow you to delete multiple branches.

Alternately if your branches are named uniformly you can write a small (shell) script to delete the branches one at a time.

Manoj Govindan
i'm a totally inexperienced shell scripter (but familiar w/ a few other web languages). any hint at this would be a great tutorial...if you have the time.
Doug
Others have given better answers in this regard. See for e.g. http://stackoverflow.com/questions/3670355/can-you-delete-multiple-branches-in-one-command-with-git/3670466#3670466
Manoj Govindan
From the manpage you linked to: "With a -d or -D option, <branchname> will be deleted. You may specify more than one branch for deletion."
Jefromi
+1  A: 

Not with that syntax. But you can do it like this:

git branch -D 3.2 3.2.1 3.2.2

Basically, git branch will delete multiple branch for you with a single invocation. Unfortunately it doesn't do branch name completion. Although, in bash, you can do:

git branch -D `git branch | grep -E '^3\.2\..*'`
slebetman
The completion on `git branch -D/-d` works fine for me. Might want to update yours (maybe from the most recent git.git contrib directory).
Jefromi
Instead of `git branch | ...` you could use `$(git for-each-ref --format='%(refname:short)' refs/heads/3.*)`. It's longer, I know, but it's guaranteed to be suitable output, while `git branch` has pretty output with things like `*` and `->` (for symrefs) which can mess you up in scripts/one-liners.
Jefromi
+3  A: 
git branch  | cut -c3- | egrep "^3.2" | xargs git branch -D
  ^                ^                ^         ^ 
  |                |                |         |--- create arguments
  |                |                |              from standard input
  |                |                |
  |                |                |---your regexp 
  |                |
  |                |--- skip asterisk 
  |--- list all 
       local
       branches

EDIT:

A safer version (suggested by Jakub Narębski and Jefromi), as git branch output is not meant to be used in scripting:

git for-each-ref --format="%(refname:short)" refs/heads/3.2\* | xargs git branch -D

... or the xargs-free:

git branch -D `git for-each-ref --format="%(refname:short)" refs/heads/3.2\*`
gawi
Do not use `git branch` output for scripting. Use `git for-each-ref`.
Jakub Narębski
+1  A: 

Use

git for-each-ref --format='%(refname:short)' 'refs/heads/3.2.*' |
   xargs git branch -D
Jakub Narębski