Skip to content

Instantly share code, notes, and snippets.

@charrisbc
Forked from ncancelliere/prune_git_branches.sh
Last active September 28, 2015 20:19
Show Gist options
  • Save charrisbc/c733b9841fa147fd8997 to your computer and use it in GitHub Desktop.
Save charrisbc/c733b9841fa147fd8997 to your computer and use it in GitHub Desktop.
Remove local git branches that have been merged
#/usr/local/bin/zsh
# Credit to http://snippets.freerobby.com/post/491644841/remove-merged-branches-in-git for the script on which this is based
current_branch=$(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
if [ "$current_branch" != "master" ]; then
echo "WARNING: You are on branch $current_branch, NOT master."
fi
echo "Fetching merged branches..."
git remote prune origin
local_branches=$(git branch --merged | grep -v 'master$' | grep -v "$current_branch$")
if [ -z "$local_branches" ]; then
echo "No existing branches have been merged into $current_branch."
else
echo "This will remove the following branches:"
if [ -n "$local_branches" ]; then
echo "$local_branches"
fi
echo "###################"
echo "staging and production will not be deleted"
read "choice?Continue? (y/n): "
echo ""
if [[ "$choice" == "y" ]] || [[ "$choice" == "Y" ]]; then
# Remove local branches
git branch -d `git branch --merged | grep -v 'master$' |grep -v "production" |grep -v "staging"| grep -v "$current_branch$" | sed 's/origin\///g' | tr -d '\n'`
else
echo "No branches removed."
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment