Skip to content

Instantly share code, notes, and snippets.

@libook
Created June 7, 2018 02:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save libook/610b9b53bc5b4b972644edd56e624bbc to your computer and use it in GitHub Desktop.
Save libook/610b9b53bc5b4b972644edd56e624bbc to your computer and use it in GitHub Desktop.
Remove abandoned branch
#!/bin/zsh
# Warning! This script will remove all local branches that not tracking remote branches.
git fetch --prune
echo "*******"
echo "Branches to remove:\n"
# TODO Reuse code and unify solution.
git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}'
git branch -vv | grep -v '\[origin/' | awk '{print $1}'
echo "\nThis will remove all branches that doesn't exist on remote!"
while true; do
read "yn?Continue anyway? Input 'Yes' to do this. "
case $yn in
Yes* )
git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}' | xargs git branch -d
git branch -vv | grep -v '\[origin/' | awk '{print $1}' | xargs git branch -d
break
;;
* )
echo "Not 'Yes'. Canceled."
exit
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment