Skip to content

Instantly share code, notes, and snippets.

@krcm0209
Created March 10, 2022 17:13
Show Gist options
  • Save krcm0209/d134971e55e4f0cbdeae2ee1886ffe36 to your computer and use it in GitHub Desktop.
Save krcm0209/d134971e55e4f0cbdeae2ee1886ffe36 to your computer and use it in GitHub Desktop.
Helper script to delete local git branches which no longer exist on the remote.
#!/bin/sh
git fetch -p
branches_with_gone_remote=$(
git for-each-ref --format '%(refname) %(upstream:track)' refs/heads |
awk '$2 == "[gone]" {sub("refs/heads/", "", $1); print $1}'
)
for branch in ${branches_with_gone_remote}
do
git branch -D $branch
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment