Skip to content

Instantly share code, notes, and snippets.

@cafuego
Forked from larowlan/git-cleanup.sh
Last active August 29, 2015 14:07
Show Gist options
  • Save cafuego/d150d35b4a4349cef6c0 to your computer and use it in GitHub Desktop.
Save cafuego/d150d35b4a4349cef6c0 to your computer and use it in GitHub Desktop.
#!/bin/bash
# This has to be run from master
git checkout master
# Update our list of remotes
git fetch
# Prune stale local branches already removed from origin
git remote prune origin
# Remove local fully merged branches
git branch --merged master | grep -v 'master$' | grep -v 'releases$' | xargs git branch -d
# Show remote fully merged branches
echo "The following remote branches are fully merged and will be removed:"
git branch -r --merged master | sed 's/ *origin\///' | grep -v 'master$' | grep -v 'releases$'
read -p "Continue (y/N)? "
case $REPLY in
y|Y)
# Remove remote fully merged branches
git branch -r --merged master | sed 's/ *origin\///' \
| grep -v 'master$' | grep -v 'releases$' | xargs -I% git push origin :%
echo "Done!"
echo "Obsolete branches are removed"
;;
*)
echo "Abort."
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment