Skip to content

Instantly share code, notes, and snippets.

@johnwgillis
Last active January 7, 2020 19:23
Show Gist options
  • Save johnwgillis/989a57c24902f3c273f8788491876f12 to your computer and use it in GitHub Desktop.
Save johnwgillis/989a57c24902f3c273f8788491876f12 to your computer and use it in GitHub Desktop.
How to delete all branches except for master locally and on a remote

Delete all local branches except for master

git branch | grep -v 'master$' | xargs git branch -D

Delete all remote branches except for master

REMOTE_NAME="origin"
command -v parallel || brew install parallel
git branch -r | grep $REMOTE_NAME/ | grep -v 'master$' | grep -v HEAD | cut -d/ -f2- | parallel git push $REMOTE_NAME --delete

Need to add another exclusion beyond master?

Just add the following after grep -v 'master$' to one of the above two commands:

 | grep -v 'branch_name_to_exclude'
@drmaas
Copy link

drmaas commented Jan 7, 2020

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment