If you use any git workflow such as git flow, you end up with several branches like feature, release and hotfix that become stale as they are merged with other branches.

So, when you need to look for a specific branch using the git branch command, you could waste time skimming through a big list of branch names.

One way to handle that is to use git branch --merged, which lists only the branches that have already been merged, and then copy the name of each one and delete them with git branch -d branch-name. That works fine if you do that every time you merge a branch, but if you're like me, you'll quickly forget to do this, and once you realize it, you'll have several branches on the list again.

Now, to remove all merged branches at once, we can use the following command:

git branch --merged | egrep -v "(^\*|master|develop)" | xargs git branch -d

This will remove all branches that have been merged with exception of master and develop. You can even create an alias to in your shell environment to not have to type or copy that every time you need it.

Careful with the -d flag. If you change it to  -D (capital case), you might end up removing unmerged branches and losing hours of work