Rafa Paez
Rafa Paez

Reputation: 4860

Delete in bulk local git branches that contains a string

Having some local git branches, let’s say for example:

…
Case001-FeatureA-doX
Case002-FeatureA-doX
Case001-FeatureB-doX
Case002-FeatureB-doX
…

How can I delete from the terminal those ones that contains “FeatureA” or “Case001”?

I guess it must to be something like git branch -l | grep "FeatureA" … but I do not know how continue.

Update with the solution that worked for me

$ git for-each-ref --format='git branch -d %(refname:short)' refs/heads/Case001-* | sh -x

Upvotes: 0

Views: 247

Answers (2)

jthill
jthill

Reputation: 60442

git for-each-ref --format='git branch -d %(refname:short)' \
      refs/heads/*-FeatureA-dox refs/heads/Case001-*-dox \
| sh -x

Upvotes: 1

fredtantini
fredtantini

Reputation: 16566

I don't have git at work, but I think that git -d can take more than one branch so

git -d Case001*

could work. Otherwise

git branch --merged | grep "FeatureA" | xargs git branch -D

xargs being the key point. You can list your branch the way you want.

Upvotes: 1

Related Questions