Skip to content

Instantly share code, notes, and snippets.

@bbelyeu
Created June 11, 2013 14:32
Show Gist options
  • Save bbelyeu/5757303 to your computer and use it in GitHub Desktop.
Save bbelyeu/5757303 to your computer and use it in GitHub Desktop.
#! /bin/bash
# @link https://coderwall.com/p/fkk2lw
if [ "$1" == "" ]; then
TRUNK="integration"
else
TRUNK=$1
fi
if [ "$2" == "" ]; then
REMOTE_PREFIX="bb/"
else
REMOTE_PREFIX=$2
fi
if git checkout $TRUNK > /dev/null 2>&1; then
echo "Successfully checked out $TRUNK !"
else
echo "Cannot checkout $TRUNK - check branch name and try again."
exit 1
fi
# Update our list of remotes
echo "Updating remotes and pruning origin..."
git remote update > /dev/null 2>&1
git remote prune origin > /dev/null 2>&1
# Remove local fully merged branches
echo "Cleaning up local branches:"
if git branch --merged $TRUNK | grep -v $TRUNK | grep -v 'master'; then
read -p "Delete local branches (y/n)? "
if [ "$REPLY" == "y" ]
then
git branch --merged $TRUNK | grep -v $TRUNK | grep -v 'master' | xargs git branch -d
fi
else
echo "No local branches to clean up!"
fi
# Show remote fully merged branches
if git branch -r --merged $TRUNK | grep $REMOTE_PREFIX | sed 's/ *origin\///' | grep -v $TRUNK | grep -v 'master' > /dev/null 2>&1; then
echo "The following remote branches are fully merged and will be removed:"
git branch -r --merged $TRUNK | grep $REMOTE_PREFIX | sed 's/ *origin\///' | grep -v $TRUNK | grep -v 'master'
read -p "Continue (y/n)? "
if [ "$REPLY" == "y" ]
then
# Remove remote fully merged branches
git branch -r --merged $TRUNK | grep $REMOTE_PREFIX | sed 's/ *origin\///' | grep -v $TRUNK \
| grep -v 'master' | xargs -I% git push origin :%
echo "Done!"
fi
else
echo "No remote branches to clean up!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment