Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fnavalca/8a92b2a038bbffd5bd27edba4cc10df4 to your computer and use it in GitHub Desktop.
Save fnavalca/8a92b2a038bbffd5bd27edba4cc10df4 to your computer and use it in GitHub Desktop.
Remove all branches merged to master from local

First of all, you must to be updated with your remote.

After that, the command is:

 git branch --merged | grep -v "\*" | grep -v master | grep -v dev | xargs -n 1 -I {} git branch -d {}

Explanation:

  • git branch --merged: list only branches merged into HEAD of current branch
  • grep -v "\*": remove the current branch from the list (we do not/can not remove it)
  • grep -v master: remove master branch from the list
  • grep -v dev: remove dev* branch from the list (useful if you are working with GitFlow or develop branch)
  • xargs -n 1 -I {} git branch -d {}: we will remove branch per branch (because we set the -n to 1).

If you want to list all branches that this command will delete, remove the last part:

 git branch --merged | grep -v "\*" | grep -v master | grep -v dev

Originally created in @artefactop 's Gist

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