Created
June 30, 2021 12:55
-
-
Save zlondrej/26f5a4106ee43f054518b1b0002463b3 to your computer and use it in GitHub Desktop.
Prune local GIT branches no longer tracked by remote
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
#!/usr/bin/bash | |
# Removes local branches that are no longer tracked on remote | |
set -e | |
branches=($(git branch -vv | grep ": gone" | grep -v "^+" | sed -r 's/^ *([^ ]+).*$/\1/')) | |
if (( ${#branches[@]} == 0 )); then | |
echo "Nothing to prune" | |
elif [[ "${1}" == "--force" ]]; then | |
echo "Deleting branches:" | |
for branch in "${branches[@]}"; do | |
echo " - ${branch}" | |
git branch -D "${branch}" | |
done | |
else | |
echo "Branches to delete (use --force):" | |
for branch in "${branches[@]}"; do | |
echo " - ${branch}" | |
done | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment