Skip to content

Instantly share code, notes, and snippets.

@bricooke
Created May 2, 2013 23:06
Show Gist options
  • Save bricooke/5506142 to your computer and use it in GitHub Desktop.
Save bricooke/5506142 to your computer and use it in GitHub Desktop.
my cleanbranches script
#!/bin/bash
# Started from http://stackoverflow.com/questions/3184555/cleaning-up-old-remote-git-branches
# BTC edited to not prompt if there weren't any remote branches to remove and to
# not `say` that it's finished
# This has to be run from master
git checkout master > /dev/null 2>&1
# Update our list of remotes
git fetch
git remote prune origin
# Remove local fully merged branches
git branch --merged master | grep -v 'master$' | xargs git branch -d
# Ask if you want to remove *all* non-master local branches
branches=$(git branch | grep -v 'master$')
if [ "$branches" != "" ]
then
echo "WARNING: BRIAN WAKE UP: READ: THINK: The following local branches are *not* fully merged and will be removed:"
echo $branches
read -p "DELETE? (yesdoit/n)? "
if [ "$REPLY" == "yesdoit" ]
then
git branch | grep -v 'master$' | xargs git branch -D
echo "Gonezo!"
fi
fi
# Show remote fully merged branches
branches=$(git branch -r --merged master | sed 's/ *origin\///' | grep -v 'master$')
if [ "$branches" == "" ]
then
exit 0
fi
echo "The following remote branches are fully merged and will be removed:"
echo $branches
read -p "Continue (y/n)? "
if [ "$REPLY" == "y" ]
then
# Remove remote fully merged branches
git branch -r --merged master | sed 's/ *origin\///' \
| grep -v 'master$' | xargs -I% git push origin :%
echo "Done!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment