Skip to content

Instantly share code, notes, and snippets.

@douglascayers
Last active March 10, 2020 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save douglascayers/11d1772adce4392e819fa5be035981b9 to your computer and use it in GitHub Desktop.
Save douglascayers/11d1772adce4392e819fa5be035981b9 to your computer and use it in GitHub Desktop.
Delete local and remote branches
# 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})
# 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