-
-
Save mkyed/1593581 to your computer and use it in GitHub Desktop.
clean-merged-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 | |
# | |
## | |
## Delete merged Git branches from the origin remote. | |
## | |
## Options: | |
## -f Really delete the branches. Without this branches are shown | |
## but nothing is deleted. | |
## | |
# stop on error | |
set -e | |
# show usage | |
[ "$1" = "--help" ] && { | |
grep '^## ' <"$0" | |
exit 0 | |
} | |
# fetch and prune remote branches | |
git fetch origin --prune | |
# grab list of merged branches | |
branches=$( | |
git branch -a --merged | | |
grep remotes/origin/ | | |
grep -v -E "/(master|test|staging|production)" | | |
sed 's@remotes/origin/@@' | |
) | |
# bail out with no branches | |
[ -z "$branches" ] && { | |
echo "# No merged branches detected" 1>&2 | |
exit 0 | |
} | |
# delete the branches or just show what would be done without -f | |
if [ "$1" = -f ]; then | |
git push origin $(echo "$branches" | sed 's/^ */:/') | |
else | |
echo "# These branches will be deleted:" | |
echo "$branches" | |
echo "" | |
echo "# Run with option '-f' if you're sure." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment