-
-
Save roblevintennis/4356196 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function remove_merged_git_branches { | |
# Following sed uses fact that current branch has a '*' in front of it | |
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 | |
# Gets remove & local merged branches (but not master, nutmeg, sugar7, current branch, etc.) | |
remote_branches=$(git branch -r --merged | grep -v '/master$' | grep -v '/toffee$' | grep -v '/sugar7$' | grep -v '/nutmeg$' | grep -v "/$current_branch$") | |
local_branches=$(git branch --merged | grep -v 'master$' | grep -v 'toffee$' | grep -v 'sugar7$' | grep -v 'nutmeg$' | grep -v "$current_branch$") | |
if [ -z "$remote_branches" ] && [ -z "$local_branches" ]; then | |
echo "No existing branches have been merged into $current_branch." | |
else | |
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 `git branch -r --merged | grep -v '/master$' | grep -v '/toffee$' | grep -v '/sugar7$' | grep -v '/nutmeg$' | grep -v "/$current_branch$" | sed 's/origin\//:/g' | tr -d '\n'` | |
# Remove local branches | |
git branch -d `git branch --merged | grep -v 'master$' | grep -v 'toffee$' | grep -v 'sugar7$' | grep -v 'nutmeg$' | 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