Skip to content

Instantly share code, notes, and snippets.

@alexejk
Created December 4, 2019 11:26
Show Gist options
  • Save alexejk/8136fc412945c86030448f56dc6705fb to your computer and use it in GitHub Desktop.
Save alexejk/8136fc412945c86030448f56dc6705fb to your computer and use it in GitHub Desktop.
Simple script to prune local branches
#!/bin/sh
# This script removes any local branch that isn't associated with remote branch.
# As part of the run, it will cleanup any stale references to remote branches (remove tracking for branches that are deleted on remote).
# Script is provided AS IS and can cause loss of work if you do not understand what it does.
echo "This will delete any *local* branch that is not available on remote!"
echo "Abort if you have any local branch not yet pushed to remote that you want to keep."
read -p "Continue? (Y/n)" -n 1 -r input
echo # Move to new line
case $input in
[yY])
echo "Fetching remote..."
git fetch -p
echo "Cleaning up local..."
for branch in `git branch -vv | grep ': gone]' | awk '{print $1}'`; do git branch -D $branch; done
;;
*)
echo "Aborted..."
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment