Skip to content

Instantly share code, notes, and snippets.

@ddfreiling
Last active February 2, 2016 14:22
Show Gist options
  • Save ddfreiling/57ed340491baa20b153b to your computer and use it in GitHub Desktop.
Save ddfreiling/57ed340491baa20b153b to your computer and use it in GitHub Desktop.
#!/usr/bin/bash
# This script deletes all local branches which has been fully merged
# with $master_branch and optionally from remote/origin too.
master_branch=master
current_branch=$(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
if [ "$current_branch" != "$master_branch" ]; then
printf "ERROR: You are on branch $current_branch, NOT $master_branch."
exit
fi
local_merged=$(git branch --merged | grep -v "\*" | grep -v master | grep -v develop | grep -v release/*)
printf "\nThe following branches has been merged into '$master_branch'\nand will be deleted locally:\n$local_merged\n\n"
read -p "Continue? (y/n): " -n 1 choice
if [ "$choice" != "y" ]; then exit; fi
printf "\nDeleting merged local branches...\n"
echo $local_merged | xargs -n 1 git branch -d
printf "\nDo you want to delete these branches from remote/origin too?\n"
read -p "Continue? (y/n): " -n 1 choice
if [ "$choice" != "y" ]; then exit; fi
echo $local_merged | xargs -n 1 git push origin --delete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment