Delete local branches not on remote using Git

· Thomas Taylor

After merging changes or working on temporary branches, users may retain a list of orphaned git branches on their local machine. Rather than deleting each branch one-by-one, running a simple script can clear them out.

Delete local branches not found on remote

The one-liner script to delete local branches not found one remote:

1git fetch -p && 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

This works by detected if a branch is [gone] from remote.


Reply to this post by email ↪