Skip to content

Instantly share code, notes, and snippets.

@zlondrej
Created June 30, 2021 12:55
Show Gist options
  • Save zlondrej/26f5a4106ee43f054518b1b0002463b3 to your computer and use it in GitHub Desktop.
Save zlondrej/26f5a4106ee43f054518b1b0002463b3 to your computer and use it in GitHub Desktop.
Prune local GIT branches no longer tracked by remote
#!/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