Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all git local branches that start with string

I want to list all local branches (eventually delete but for safety's sake...) that ONLY begin with abc. The thing is that this kind of works. But if no branch starts with "abc" then it lists ALL of the branches. which is what I don't want to end up doing (deleting all my local branches)

git for-each-ref --format="%(refname:short)" refs/heads/abc\* | xargs git branch --list
like image 214
user3521314 Avatar asked Mar 15 '23 22:03

user3521314


1 Answers

To list all branches that begin with "abc":

git branch --list "abc*"

So if you want to delete them, then run the following:

git branch --list "abc*" | xargs --no-run-if-empty git branch --delete

You can append the --force flag to the command above if you want to live dangerously.

like image 180
kzh Avatar answered Mar 25 '23 09:03

kzh