TIL: How to clean up merged git branches in the local copy

Published Sun 06 October 2024 in personal

by Bryan Weber

Today I learned how to delete local git branches that have been merged to the main remote branch. There are a bunch of sites that give instructions or packages for this, but I always have to search for them. So this is meant to be my reminder to myself of how I like to do this. This approach will work on Linux and macOS (bash or zsh).

git fetch --prune \
    && for branch in $(git for-each-ref --format '%(refname) %(upstream:track)' refs/heads | awk '$2 == "[gone]" {sub("refs/heads/", "", $1); print $1}'); \
    do git branch -D $branch; \
    done;

First, fetch the default remote and prune any remote branches. Then list all the branches, test if they have the string [gone] in them, and delete the ones that do. Easy peasy one liner.