git branch -D BRANCH_NAME
command to delete a specific branch in git repository.
ButHow should we clean up a repository which has a ton of old branches ?
Git support it in a single line command.
git branch -D BRANCH1_NAME BRANCH2_NAME BRANCH3_NAME
Unfortunately, Git does not support deleting branches with branh name completion. Although, in bash you can do that with Git and
grep
command combination.For example, I want to delete branches which have PREFIX- in name. I used the following command:
git branch -D PREFIX-*
but git return error: branch 'PREFIX-*' not found.
We should use the following instead to make git works:
git branch -D `git branch | grep -E 'PREFIX-*'`