Remove all removed git branches

Posted on Fri 28 July 2023 in Tools

When you work with feature branches in Git, you may have a lot of branches on your computer that is removed from the origin repo. Instead of manually cleaning the removed branches I added a new commando/alias to Git, git gone.

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

It will remove all branches that don't exist in the upstream repo.