Skip to content

Instantly share code, notes, and snippets.

@piranha
Created February 20, 2013 09:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piranha/4994378 to your computer and use it in GitHub Desktop.
Save piranha/4994378 to your computer and use it in GitHub Desktop.
Script to cleanup git branches
#!/bin/sh
current_branch=$(git symbolic-ref HEAD | awk -F / '{print $3}')
if [ "$current_branch" != "master" ]; then
echo "WARNING: You are on branch $current_branch, NOT master."
fi
echo "Fetching merged branches..."
git remote prune origin
remote_branches=$(git branch -r --merged | grep -v '/master$' | grep -v "/$current_branch$")
local_branches=$(git branch --merged | grep -v 'master$' | grep -v "$current_branch$")
if [ -z "$remote_branches" ] && [ -z "$local_branches" ]; then
echo "No existing branches have been merged into $current_branch."
exit
fi
echo "This will remove the following branches:"
if [ -n "$remote_branches" ]; then
echo "$remote_branches"
fi
if [ -n "$local_branches" ]; then
echo "$local_branches"
fi
read -p "Continue? (y/n): " -n 1 choice
echo
if [ "$choice" == "y" ] || [ "$choice" == "Y" ]; then
# Remove remote branches
git push origin $(echo $remote_branches | sed 's/origin\//:/g' | tr -d '\n')
# Remove local branches
git branch -d $(echo $local_branches | sed 's/origin\///g' | tr -d '\n')
else
echo "No branches removed."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment