How to delete a whole folder of branches in git

Viewed 217

I have many local branches under different folders eg. bug/the-branch or feat/the-branch.

How can i remove every branch under a certain name?

3 Answers

List all branches under the name:

git for-each-ref --format="%(refname:short)" refs/heads/feat/

and delete them one by one:

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

Please be advised you cannot remove the current branch so first checkout a different branch not under the top name.

You can delete all branches within the same pattern by doing:

git branch -D $(git branch -a | grep <my_pattern> | awk '{print $1}')

where <my_pattern> is your name.

For example to remove all branches feat/test:

git branch -D $(git branch -a | grep 'feat/test' | awk '{print $1}')

To delete all branches from a specific folder:

git branch -d $(git for-each-ref --format="%(refname:short)" refs/heads/featureFolderName/)

Or to simplify the code, another shorter way.

git branch -d $(git for-each-ref refs/heads/featureFolderName/)
Related