Skip to content

Instantly share code, notes, and snippets.

@bbelyeu
Created June 23, 2016 20:22
Show Gist options
  • Save bbelyeu/5abd53f491cec14f25614a46d2da5f12 to your computer and use it in GitHub Desktop.
Save bbelyeu/5abd53f491cec14f25614a46d2da5f12 to your computer and use it in GitHub Desktop.
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 fsck --unreachable
echo "These commits are unreachable and will be deleted if we continue with the cleanup."
read -p "Press [enter] to continue or Cntrl-C to bail out"
git remote prune origin > /dev/null 2>&1
git gc
# 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