Created
June 10, 2013 12:10
-
-
Save ansman/5748281 to your computer and use it in GitHub Desktop.
Shell function to remove fully merged git branches
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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