Skip to content

Instantly share code, notes, and snippets.

@ansman
Created June 10, 2013 12:10
Show Gist options
  • Save ansman/5748281 to your computer and use it in GitHub Desktop.
Save ansman/5748281 to your computer and use it in GitHub Desktop.
Shell function to remove fully merged git branches
function git-purge-branches {
CURRENT_BRANCH=`git rev-parse --abbrev-ref HEAD`
if ! [ "$CURRENT_BRANCH" = 'master' ]; then
echo 'Please stash your work and checkout master'
return
fi
git fetch -p
BRANCHES=`git branch --merged master | grep --invert-match master`
if [ $BRANCHES ]; then
echo 'The following local branches will be removed: '
echo $BRANCHES
read "REPLY?Continue (y/n)? "
if [ "$REPLY" = "y" ]; then
echo $BRANCHES | xargs git branch -d
fi
fi
BRANCHES=`git branch -r --merged master | grep --invert-match master | grep "origin" | sed -e "s#origin/##"`
if [ $BRANCHES ]; then
echo 'The following remote branches will be removed: '
echo $BRANCHES
read "REPLY?Continue (y/n)? "
if [ "$REPLY" = "y" ]; then
echo $BRANCHES | xargs -I% git push origin :%
fi
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment