Skip to content

Instantly share code, notes, and snippets.

@kipras
Forked from michaeltwofish/git-branch-rm-merged
Last active December 14, 2015 14:08
Show Gist options
  • Save kipras/5098074 to your computer and use it in GitHub Desktop.
Save kipras/5098074 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# From: https://gist.github.com/michaeltwofish/5096740
# Kipras:
# - modified to run against develop instead of master
# - made a small fix to run within Git bash on Windows
# - uncommented local branch deleting (in original this was commented out,
# deleting only remote branches for some reason)
#
# 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 develop
# TODO allow test against any branch
notice "Checking out develop ..."
git checkout develop
# 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 develop | grep -v 'develop\|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 develop | sed 's/ *origin\///' | grep -v 'develop\|dev\|master$'
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 develop | sed 's/ *origin\///' \
| grep -v 'develop\|dev\|master$' | xargs --replace=% git push origin :%
notice "Done!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment