Skip to content

Instantly share code, notes, and snippets.

@michaeltwofish
Created March 6, 2013 04:38
Show Gist options
  • Save michaeltwofish/5096740 to your computer and use it in GitHub Desktop.
Save michaeltwofish/5096740 to your computer and use it in GitHub Desktop.
Script to remove branches that have been fully merged to master, except dev
#!/bin/sh
# Remove branches that have been fully merged to master, except dev
# Based on http://devblog.springest.com/a-script-to-remove-old-git-branches
# Helpber function to colourise output
# black='\E[0;30m' red='\E[0;31m' green='\E[0;32m' yellow='\E[0;33m'
# blue='\E[0;34m' magenta='\E[0;35m' cyan='\E[0;36m' white='\E[0;37m'
message() {
message=$1
color=${2:-$white} # Defaults to white, if not specified.
echo -e "${color}${message}"
# Reset text attributes to normal without clearing screen.
tput sgr0
return
}
notice() {
yellow='\E[0;33m'
message "$1" $yellow
}
error() {
red='\E[0;31m'
message "$1" $red
}
git status > /dev/null 2>&1
if [ "$?" -ne "0" ]
then
error "fatal: Not a git repository"
exit $?
fi
clear
# This has to be run from master
# TODO allow test against any branch
notice "Checking out master ..."
git checkout master
# Update our list of remotes
# TODO allow pruning of non-origin remotes
notice "Updating remote branch list ..."
git fetch
git remote prune origin
# Remove local fully merged branches
notice "Removing local fully merged branches ..."
# git branch --merged master | grep -v 'master$' | xargs git branch -d
# Show remote fully merged branches
notice "Finding remote fully merged branches ..."
echo "The following remote branches are fully merged and will be deleted:"
git branch -r --merged master | sed 's/ *origin\///' | grep -v 'master\|dev$'
read -p "Delete remote branches (y/n)? "
if [ "$REPLY" != "y" ]
then
notice "Aborted"
else
# Remove remote fully merged branches
notice "Removing remote fully merged branches ..."
git branch -r --merged master | sed 's/ *origin\///' \
| grep -v 'master\|dev$' | xargs -I% git push origin :%
notice "Done!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment