Cleaning Up Old Git Branches

When working on a software project with a team, odds are you're going to use git, and you'll probably also be branching off the main branch to make your changes and will need to submit a pull request to get those changes merged back into the main branch. I'd also wager your responsibilities include reviewing others' code, and it's generally a good idea to pull their changes locally to review and run the code to double check that it does what it claims to. Over time, this can lead to a build-up of dead branches on your local machine when the pull requests are merged, and the remote branches are deleted. If you don't remember to delete these branches locally, it can clutter up your branch list and make it unnecessarily difficult to find branches that you're actively working on. I did a little digging and stumbled upon this StackOverflow answer that provides a nice solution to find and delete any local branches whose remote equivalents no longer exist. I only slightly tweaked it to use xargs instead of a for loop for readability:

git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -D