Skip to main content

Git Sweep Alias - Deleting stale local remote-tracking branches

Reading time: 2 Minutes

I never remember the command for cleaning my remote branches so I decided to create an alias for them.

The alias#

git sweep

This is a shortcut for the git fetch --prune command and a script that finds branches marked as gone then deletes all of them. There's more detail about the process here:

Setting the alias#

You can use set them using the git config command or edit the git config file directly.

Here's the command to set the alias with the git config command:

git config --global alias.sweep "! git fetch -p && git for-each-ref --format '%(refname:short) %(upstream:track)' | awk '\$2 == \"[gone]\" {print \$1}' | xargs -r git branch -D"

Here's what the config file should look like the aliases added:

sweep = ! "git fetch -p && git for-each-ref --format '%(refname:short) %(upstream:track)' | awk '$2 == \"[gone]\" {print $1}' | xargs -r git branch -D"

Special Thanks#

Thank you Erik Schierboom for providing the solution. Hopefully this gets added directly into git one day. This alias is super useful for maintaining your repositories.