Created
July 31, 2013 17:19
-
-
Save vindia/6124075 to your computer and use it in GitHub Desktop.
Remove obsolete branches in git
This file contains hidden or 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
#!/bin/bash | |
# Remove all branches which are merged in to master | |
# Run this from within your repository, something like: | |
# cd /path/to/repo | |
# script/remove-obsolete-branches | |
#----- | |
BRANCH=${1:-'master'} | |
# This has to be run from master | |
git checkout $BRANCH | |
# Update our list of remotes | |
git fetch | |
git remote prune origin | |
# Remove local fully merged branches | |
git branch --merged $BRANCH | grep -v "$BRANCH$" | xargs git branch -d | |
# Remove remote fully merged branches | |
echo "The following remote branches are fully merged into $BRANCH and will be removed:" | |
git branch -r --merged $BRANCH | sed 's/ *origin\///' | grep -v "$BRANCH$" | |
read -p "Continue (y/n)? " | |
if [ "$REPLY" == "y" ] | |
then | |
git branch -r --merged $BRANCH | sed 's/ *origin\///' | grep -v "$BRANCH$" | 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