Skip to content

Instantly share code, notes, and snippets.

@harmon
Last active August 29, 2015 14:01
Show Gist options
  • Save harmon/cc455ffc9d0ed2be86d7 to your computer and use it in GitHub Desktop.
Save harmon/cc455ffc9d0ed2be86d7 to your computer and use it in GitHub Desktop.
Git Branch Pruning Script
#!/bin/bash
# From http://devblog.springest.com/a-script-to-remove-old-git-branches/
# This will remove all remote branches that have been merged into develop (excluding develop, stage, production branches)!
#
echo "The following remote branches have been removed from origin and will be pruned:"
git remote prune origin --dry-run
say "The following remote branches have been removed from origin and will be pruned:"
read -p "Continue (y/n)? " REPLY_1
if [ "$REPLY_1" == "y" ]; then
git remote prune origin
echo "Done!"
say "Old origin branches have been removed."
fi
echo "The following local branches have been merged into develop and will be removed:"
git branch --merged develop | grep -v 'develop$'
say "The following remote branches have been removed from origin and will be removed:"
read -p "Continue (y/n)? " REPLY_2
if [ "$REPLY_2" == "y" ]; then
git branch --merged develop | grep -v 'develop$' | xargs git branch -d
echo "Done!"
say "Old local branches have been removed."
fi
echo "The following remote branches are fully merged into master and will be removed:"
git branch -r --merged develop | sed 's/ *origin\///' | grep -v 'develop$' | grep -v 'production$' | grep -v 'stage$'
say "The following remote branches are fully merged into master and will be removed:"
read -p "Continue (y/n)? " REPLY_3
if [ "$REPLY_3" == "y" ]; then
git branch -r --merged develop | sed 's/ *origin\///' | grep -v 'develop$' | grep -v 'production$' | grep -v 'stage$' | xargs -I% git push origin :%
echo "Done!"
say "Obsolete branches are removed"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment