Brain Phrye

code cooking diy fiction personal photos politics reviews tools 


Git Local Branch Pruning

[ Listen to article ]

I have fetch.prune set to true globally. This means that when I pull from remotes git will delete any remote branches that have been removed since my last pull.

For gitlab merge request workflows this is really handy. However it only cleans up the remote branches, not mine. To handle local ones, I’ve aliased this command to prune-origin:

1
2
3
for b in $(git branch -l --format="%(refname:lstrip=2)"); do
  git show-ref --verify --quiet refs/remotes/origin/$b || git branch -d $b;
done

However I’m not super happy with it. If anyone has better ideas for cleaning up local branches, please pass them on. I still haven’t sorted comments, but feel free to send me an email or ping me on twitter.