The Gitflow branching strategy is great, but I end up with a lot of junk branches. Here’s what I do to clean them up.

git branch | grep -E -v '(develop|master)' | xargs git branch -D

git

git branch lists branches

result

  bananas
* develop
  feature-a
  feature-b
  master

| kicks the result to the next program

grep

grep searches the list of branches

-E forces grep to behave as egrep, which makes the regex part easier

-v selects lines that do not match the pattern

(develop|master) regex for the branches we want to keep

result

  bananas
  feature-a
  feature-b

| kicks the result to the next program

xargs

xargs sends each branch name as an argument to the next command

git branch -D forces Git to delete the branch

result

Deleted branch bananas (was 1b61fdc).
Deleted branch feature-a (was 1b61fdc).
Deleted branch feature-b (was 1b61fdc).