Last active
April 13, 2016 02:55
-
-
Save kimpepper/d5703d39f70353dab8a76d66089f3ff5 to your computer and use it in GitHub Desktop.
Prune merged git branches from local and remote
This file contains hidden or 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
# Prune merged git branches from local and remote | |
# example usage: gprune 8.x-1.x | |
gprune() { | |
DEFAULT_BRANCH=master | |
PROTECTED_BRANCH_PATTERN='|master|releases|7.x.*|8.x.*' | |
# Allow the base branch to be passed in as 1st param. | |
base_branch=${1-$DEFAULT_BRANCH} | |
echo "Base branch: $base_branch" | |
# Regex for protected branches. | |
protected_branches=$base_branch$PROTECTED_BRANCH_PATTERN | |
echo "Protected branches regex: $protected_branches" | |
# This has to be run from base branch | |
git checkout $base_branch | |
# Update our list of remotes | |
git fetch | |
git remote prune origin | |
# Remove local fully merged branches | |
git branch --merged $base_branch | grep -vE "$protected_branches" | xargs git branch -d | |
# Show remote fully merged branches | |
echo "The following remote branches are fully merged and will be removed:" | |
git branch -r --merged $base_branch \ | |
| sed 's/ *origin\///' \ | |
| grep -vE "$protected_branches" | |
read -p "Continue (y/n)? " | |
if [ "$REPLY" == "y" ] | |
then | |
# Remove remote fully merged branches | |
git branch -r --merged $base_branch \ | |
| sed 's/ *origin\///' \ | |
| grep -vE "$protected_branches" \ | |
| xargs -I% git push origin :% | |
echo "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