Last active
December 21, 2015 01:59
-
-
Save sadlerjw/6232107 to your computer and use it in GitHub Desktop.
Delete all branches that are already merged into master, both locally, and optionally in a specified remote. Execute "git rm-merged -h" for details.
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
#!/bin/bash | |
if [ "$1" == "-h" ]; then | |
echo "Usage: git rm-merged [remote]" | |
echo "Removes any local branches already merged into master" | |
echo "If a remote is supplied, it also removes any branches on that remote that have" | |
echo "been merged into that remote's master branch." | |
exit 1 | |
fi | |
# get the branch the user was already on | |
b="$(git symbolic-ref HEAD 2>/dev/null)"; | |
if [ -n "$b" ]; then | |
b="${b##refs/heads/}"; | |
fi | |
# can't delete the branch you're already on! | |
git checkout master | |
remote=$1 | |
excludes='master' | |
if [ "$remote" != "" ]; then | |
for branch in `git branch -r --merged ${remote}/master | grep $remote` | |
do | |
branch=${branch:${#remote} + 1} | |
should_exclude=false | |
for exclude in $excludes | |
do | |
if [ $branch == $exclude ]; | |
then | |
should_exclude=true | |
fi | |
done | |
if ! $should_exclude ; | |
then | |
git push $remote :$branch | |
fi | |
done | |
fi | |
# Exclude the current branch (master), which is marked with a * in the output of git branch. | |
for branch in `git branch --merged | grep -v "\*"` | |
do | |
should_exclude=false | |
for exclude in $excludes | |
do | |
if [ $branch == $exclude ]; | |
then | |
should_exclude=true | |
fi | |
done | |
if ! $should_exclude ; | |
then | |
git branch -d $branch | |
fi | |
done | |
if [ -n "$b" ]; then | |
git checkout $b | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment