Skip to content

Instantly share code, notes, and snippets.

@wojtha
Created February 20, 2020 11:02
Show Gist options
  • Save wojtha/40a8223a3304d5c7a981d3afc1936cfe to your computer and use it in GitHub Desktop.
Save wojtha/40a8223a3304d5c7a981d3afc1936cfe to your computer and use it in GitHub Desktop.
Remove local git branch when it is linked to non-existing remote branch
#!/usr/bin/env bash
# Based on https://stackoverflow.com/a/33548037
# Maybe we can add this: https://stackoverflow.com/a/32166469
echo "*** Local tracking branches with removed remote branches:"
# List files
git branch -vv | grep ': gone]'| grep -v "\*" | awk '{ print $1; }'
# Ask user
read -p "*** Remove those branches? (y/N) " -r -s -n 1
# Set default answer
[ -z "$REPLY" ] && REPLY="n"
# Delete or not
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo ">> Yes"
echo "*** Removing following local branches:"
git branch -vv | grep ': gone]'| grep -v "\*" | awk '{ print $1; }' | xargs -n 1 git branch -D
echo "*** DONE!"
else
echo ">> No"
echo "*** Nothing to do"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment