Delete multiple git branches matching a prefix
Important: this is a potentially destructive command. Please proceed cautiously and at your own risk.
I'll sometimes leave local git branches around a bit longer than I should. And, depending on your workflow, you may occasionally have a handful of ephemeral branches that need to be cleaned up.
In this example, we exclude the active branch with grep invert match (not) *
and filter out any additional branches with an escaped \|
pipe (master, staging); this is only necessary if your match pattern includes branches you want to keep. Then we match the prefix feature-
for the branches we intend to delete.
git branch -D \
$(git branch | grep -v '*\|master\|staging' | tr -d ' ' | grep -E '^feature-')
Modify the git branch
command options as needed. For example, you can specify to only include previously merged branches, etc.