Skip to content

Instantly share code, notes, and snippets.

@d3d-z7n
Last active September 16, 2020 19:54
Show Gist options
  • Save d3d-z7n/1af74ae78e0b8c4828a6c5c760ff665a to your computer and use it in GitHub Desktop.
Save d3d-z7n/1af74ae78e0b8c4828a6c5c760ff665a to your computer and use it in GitHub Desktop.
Prompt for and delete local branches that are tracking deleted remote branches
#!/usr/bin/env zsh
# Remove no longer existing remote branches.
git fetch --prune &> /dev/null
# Find all branches with a deleted remote branch.
branches=(${(@f)$(git branch -vv | grep ": gone]" | awk '{ print $1 }')})
if [[ $#branches == 0 ]]; then
print -P "No pruneable local branches found."
exit 0
fi
print -P "%F{cyan}The following local branches will be deleted:\n%B%F{red}"
print -f "%s\n" ${branches[@]}
print -nP "%b%F{cyan}\nContinue (y/n)?%f "
read -q "confirmation?"
if [[ $confirmation == "y" ]]; then
echo
git branch -D ${branches[@]}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment