Skip to content

Instantly share code, notes, and snippets.

@drbrain
Created September 24, 2015 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save drbrain/d9329694b873b1eed3b0 to your computer and use it in GitHub Desktop.
Save drbrain/d9329694b873b1eed3b0 to your computer and use it in GitHub Desktop.
#!/bin/bash
# git-cleanup-repo
#
# Author: Rob Miller <rob@bigfish.co.uk>
# Adapted from the original by Yorick Sijsling
KEEP_BRANCHES='(master|staging|ci-[0-9]+)$'
git checkout master &> /dev/null
# Make sure we're working with the most up-to-date version of master.
git fetch
# Prune obsolete remote tracking branches. These are branches that we
# once tracked, but have since been deleted on the remote.
git remote prune origin
# List all the branches that have been merged fully into master, and
# then delete them. We use the remote master here, just in case our
# local master is out of date.
git branch --merged origin/master | egrep -v "$KEEP_BRANCHES" | xargs git branch
-d
# Now the same, but including remote branches.
MERGED_ON_REMOTE=`git branch -r --merged origin/master | sed 's/ *origin\///' |
egrep -v "$KEEP_BRANCHES"`
if [ "$MERGED_ON_REMOTE" ]; then
echo "The following remote branches are fully merged and will be removed:"
echo $MERGED_ON_REMOTE
read -p "Continue (y/N)? "
if [ "$REPLY" == "y" ]; then
git branch -r --merged origin/master | sed 's/ *origin\///' \
| egrep -v "$KEEP_BRANCHES" | xargs -I% git push origin :% 2>&1 \
| grep --colour=never 'deleted'
echo "Done!"
fi
fi
git checkout master &> /dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment