Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you delete multiple branches in one command with Git?

Tags:

git

I'd like to clean up my local repository, which has a ton of old branches: for example 3.2, 3.2.1, 3.2.2, etc.

I was hoping for a sneaky way to remove a lot of them at once. 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.

like image 418
Doug Avatar asked Sep 08 '10 17:09

Doug


People also ask

How do I delete multiple branches in git?

To delete many branches based on a specified pattern do the following: Open the terminal, or equivalent. Type in git branch | grep "<pattern>" for a preview of the branches that will be deleted. Type in git branch | grep "<pattern>" | xargs git branch -D.

How do I delete branches in git?

Delete a branch with git branch -d <branch> . The -d option will delete the branch only if it has already been pushed and merged with the remote branch. Use -D instead if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet. The branch is now deleted locally.


1 Answers

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\..*'` 
like image 110
slebetman Avatar answered Sep 28 '22 04:09

slebetman