Created
February 10, 2015 22:58
-
-
Save jonmoter/d7a0492647468ba78fb7 to your computer and use it in GitHub Desktop.
Delete merged git branches
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
#!/bin/bash | |
shopt -s expand_aliases | |
function prompt_to_continue() | |
{ | |
read -p "Continue (y/n)? " | |
if [ "$REPLY" != "y" ]; then | |
exit 0 | |
fi | |
} | |
alias filter_branches="sed 's/^[ \*]*//g' | grep -Ev '^(master|release|production|staging|demo|heroku|hotfix|HEAD)'" | |
base_branch="master" | |
git checkout $base_branch | |
# Update our list of remotes | |
git fetch || exit -1 | |
git remote prune origin || exit -1 | |
# Remove local fully merged branches | |
merged_branches=$(git branch --merged $base_branch | filter_branches) | |
if [ "$merged_branches" = "" ]; then | |
echo 'No local branches need deletion' | |
else | |
echo '' | |
echo "The following LOCAL branches are fully merged and will be removed" | |
for branch in $merged_branches ; do | |
echo " $branch" | |
done | |
echo '' | |
prompt_to_continue | |
for branch in $merged_branches ; do | |
git branch -d $branch || exit -1 | |
done | |
fi | |
# Show remote fully merged branches | |
merged_branches=$(git branch -r --merged $base_branch | sed 's/ *origin\///' | filter_branches) | |
if [ "$merged_branches" = "" ]; then | |
echo 'No remote branches need deletion' | |
else | |
echo '' | |
echo "The following remote branches are fully merged and will be removed:" | |
for branch in $merged_branches ; do | |
echo " $branch" | |
done | |
echo '' | |
prompt_to_continue || exit -1 | |
# Remove remote fully merged branches | |
for branch in $merged_branches ; do | |
git push origin :$branch || exit -1 | |
done | |
echo "Obsolete branches are removed" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment