Last active
March 10, 2020 21:25
-
-
Save douglascayers/11d1772adce4392e819fa5be035981b9 to your computer and use it in GitHub Desktop.
Delete local and remote 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
# You'll need to keep one branch. | |
# Specify that branch's name here. | |
BRANCH_TO_KEEP=master | |
# Switch to the branch you're keeping | |
git checkout ${BRANCH_TO_KEEP} | |
# Delete all other local branches | |
git branch -D $(git branch | grep -v -e ${BRANCH_TO_KEEP}) |
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
# Ensure know about all remote branches | |
git fetch --all | |
# You'll have to keep at least one branch, | |
# often the default branch for the repo. | |
# Specify that branch's name here. | |
BRANCH_TO_KEEP=master | |
# Delete remote branches whose name is not $BRANCH_TO_KEEP. | |
# The `sed` command is to strip the leading 'origin/' prefix | |
# from the branch name that `git branch -r` adds. | |
git push origin --delete --force $(git branch -r | grep -v -e ${BRANCH_TO_KEEP} | sed 's/origin\///') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment