Git - Delete unused branches and remote trackers

If you're like me, and like to keep your git tree clean and free of unruly unused branches, you always click "Delete this branch" when merging a pull request on GitHub.

But what about your local machine!!?!?!? How do we keep the homegrounds nice and clean?

To remove the tracking branches (origin/whatever) after you've merged and deleted them use:
git fetch --prune

This fetching command will get the latest changes from your remote repository, while "pruning" your tree of deleted (origin/whatever) branches.

As for the merged local branches, use:
git diff --merged | grep -v master | xargs git branch -d

This command deletes your local branches that have been merged remotely. The grep -v filters out master so that you don't delete your local master branch if it's up to date with origin/master.

I just deleted about 50 excess branches with those two commands. So, needless to say, these are quite useful for me.

As a caution:

Only use those if your team is okay with deleting unused branches. My team isn't okay with that, but for personal projects, I like to keep my branch count to a minimum.