Git Local Branch Cleanup

Most of the guts were borrowed from Erik Aybar's Git Tip: Deleting Old Local Branches post.  I added some tweaks and commentary.

When using git in a normal development workflow with another team, you are going to amass a great deal of local branches.  They are harmless and cheap, but start to get in your way a bit.  I like to clean them up from time to time.  Here's how I do it.

First, use branches on the server (remote origin) to determine what is the truth.  In other words, if it's been deleted from the server but still exist locally... we want those gone (generally).  Mark these abandoned branches like so:

git remote prune origin 

Then physically delete them with:

git branch -vv | grep 'origin/.*: gone]' | awk '{print $1}' | xargs git branch -d --force

Execute this from your favorite bash terminal - git bash on Windows.

Kelly Brownsberger

Kelly Brownsberger

I'm a passionate technologist, father, husband, and life long learner. I know how to laugh and try to always leave things better than I found them.
Photo by Blake Richard Verdoorn on Unsplash