samfharrison.com

How I Git

Living Artifact


This is basically so I don't forget things.

Pruning local branches

I use git branch a lot to check branch names before switching to them, and I really hate it when the list gets clogged up with 20+ old branches.

Git has a native way to prune stale remote-tracking branches:

git fetch --prune

But that only removes the remote references - like origin/old-branch - it doesn't delete the actual branch, even if the remote branch it was tracking no longer exists.

So to clean those up, I use this:

git fetch --prune && \ git branch --format='%(refname:short) %(upstream:track)' | \ awk '$2 == "[gone]" {print $1}' | \ xargs git branch -d