Last active
February 1, 2018 01:22
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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