1 min read

Deleting redundant git branches!

Every so often I find my local repositories being a mess of old undeleted branches! I generally have two approaches for that:

  1. Delete all branches but currently active — I rarely need production locally anyway, just make sure you have pushed your most recent stuff online. Easy to substitute what the actual branch to keep is:
git branch | grep -v "dev" | xargs git branch -D
  1. An alternative option — delete all branches that don't have an online upstream. Very useful to get rid of the branches you've merged in already.  As with most git commands, you can change a lot with simple flags. I mainly use two variations, one for safely deleting (meaning a warning will be displayed if some branches are not fully merged) and one that will force delete all of them.
Casing at times makes all the difference.
git fetch -p && git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -d
# error: The branch 'fix/xxxx-less-generic-follow-up-approach' is not fully merged.If you are sure you want to delete it, run 'git branch -D fix/xxx-less-generic-follow-up-approach'.

vs

git fetch -p && git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -D
# which would basically force delete them regardless of their status