Skip to content

Instantly share code, notes, and snippets.

@brawlins
Last active July 19, 2018 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brawlins/98563783155ac28fe8e317c0a3af26c9 to your computer and use it in GitHub Desktop.
Save brawlins/98563783155ac28fe8e317c0a3af26c9 to your computer and use it in GitHub Desktop.
Shell script that deletes local git branches that were deleted on the remote
# Deletes local branches that have been deleted on the remote
# Make sure you're on master because it should never get pruned
git checkout master &> /dev/null
# Prune branches
git fetch origin --prune &> /dev/null
# List branches that have been removed from origin and write to file
git branch -vv | awk '/: gone]/{print $1}' > /tmp/branchesToPurge
# Open the file for editing
vim /tmp/branchesToPurge
# Print list of branches to be deleted
echo "The following branches will be deleted:"
cat /tmp/branchesToPurge
# Ask if we should proceed
read -p "Are you sure you want to delete these branches (y/n)? " answer
case ${answer:0:1} in
y|Y )
echo "Deleting...";
# Delete branches listed in file
xargs git branch -D < /tmp/branchesToPurge
exit;;
* )
echo "Aborted";
exit;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment