Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete git branches whose name matches a certain pattern

Tags:

git

github

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.

like image 795
Nirmalya Ghosh Avatar asked Nov 10 '15 13:11

Nirmalya Ghosh


People also ask

How do I delete multiple branches at once?

Deleting Multiple Remote Branchesegrep -v "(^\*|master|main)" - exclude branch master and main. sed 's/origin\///' - because the remote branches are prefixed with origin/ this command will filter that part out so it returns only the branch name. xargs -n 1 git push origin --delete - deletes the remaining branches.

How do I remove a specific branch in git?

The command to delete a local git branch can take one of two forms: git branch –delete old-branch. git branch -d old-branch.

How do I find out who deleted a branch?

Search for the exact branch name using the Search all branches box in the upper right. Click the link to Search for exact match in deleted branches. If there is a deleted branch that matches your search, you will be able to see which commit it pointed to when it was deleted, who deleted it, and when it was deleted.

Does git prune delete branches?

git fetch --prune is the best utility for cleaning outdated branches. It will connect to a shared remote repository remote and fetch all remote branch refs. It will then delete remote refs that are no longer in use on the remote repository.


2 Answers

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 
like image 108
hek2mgl Avatar answered Oct 12 '22 06:10

hek2mgl


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

like image 37
lzzluca Avatar answered Oct 12 '22 06:10

lzzluca