GIT: How to remove all local and remote branches except specified

Hello everyone!

Today I'm going on to tell to you the easiest way how to remove local and remote branches in GIT. 

  1. Remove all local branches except current:
    git branch | xargs git branch -D
  2. Remove all local branches except specified:
    git branch | grep -v "master" | grep -v "develop" | xargs git branch -D
  3. Remove all remote branches except protected (not tested):
    export REMOTE_NAME="origin" && git branch -r | xargs -I{} | cut -c$(echo -n $REMOTE_NAME// | wc -m)-100 | xargs git push $REMOTE_NAME --delete
  4. Remove all remote branches except specified:
    export REMOTE_NAME="origin" && git branch -r | grep -v "master" | grep -v "develop" | xargs -I{} | cut -c$(echo -n $REMOTE_NAME// | wc -m)-100 | xargs git push $REMOTE_NAME --delete

Hope this article will help you and save your day!

Enjoy!