Skip to content

Instantly share code, notes, and snippets.

@gigamonkey
Last active February 1, 2018 01:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gigamonkey/f7ac9cceb2ac8df737174369f0d9fa54 to your computer and use it in GitHub Desktop.
Save gigamonkey/f7ac9cceb2ac8df737174369f0d9fa54 to your computer and use it in GitHub Desktop.
Bash script for cleaning up local branches that have been removed from remotes
#!/bin/bash
# Prune remote branches that no longer exist on remotes
for remote in $(git remote); do
git remote prune "$remote"
done
# Delete branches whose tracking branch no longer exists
for b in $(git show-ref --heads | cut -c 53-); do
remote=$(git config "branch.$b.remote")
if [ ! -z "$remote" ]; then
x=$(git show-ref | grep "refs/remotes/$remote/$b");
if [ -z "$x" ]; then
git branch -D "$b"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment