Clean up Previously Merged Git Branches

First things first, I tend to build up a lot of local branches. No, I don’t pro-actively remove them from my machine. (Who does that extremely smart thing? 🙄) At work we use GitHub and Windows. As such I use posh-git to perform almost all my git interactions (you should too cause it’s 👌).

Our current process is:

  1. Create Feature Branch
  2. Create Pull Request
  3. Squash and Merge Pull Request
  4. Delete Feature Branch from Remote

Any developer can have any number of feature branches out at a time waiting to be merged.  As we are using a “Squash and Merge” process our branches are not *actually merged*. We delete the original branch after the squash.

But that doesn’t really matter to you. Come to think of it, it doesn’t really matter to future me. What does matter is, how can I remove local git branches that aren’t on the git remote anymore? (Remote meaning GitHub, Gitlab, Bitbucket, etc…). No, we aren’t talking about the remote tracking branches that are removed by pruning. Otherwise, one could just prune, no?

I continuously search for this, so I’m documenting it here so I NEVER HAVE TO SEARCH EVER AGAIN 😤

First of all, the answer to the question comes from this question on StackOverflow.

git checkout master; git remote update origin --prune; git branch -vv | Select-String -Pattern ": gone]" | % { $_.toString().Trim().Split(" ")[0]} | % {git branch -d $_}

If you are in a position where you Squash & Merge then you will need to replace the git branch -d with git branch -D. This will successfully remove all “:gone” branches from your local machine.

If you are using linux, and not powershell, you can use the following command taken from the same question on StackOverflow.

git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}' | xargs git branch -d

But Why?!

Any git branch you have checked out essentially has three branches.
1. The remote branch on GitHub
2. The remote tracking branch on your machine
3. The local branch you do your work on.

When we push a feature branch live (merge a pull request) we delete the “remote branch on GitHub” (#1 above).

When we run `git fetch origin –prune` we remove the remote tracking branch (taking care of #2) above.

However, taking care of #3 above often requires manual clean up. That’s where the piped command above comes into play, it takes care of cleaning up branch #3.

Why I no longer contribute to StackOverflow – Michael T. Richter

I ran into this post by Michael T. Richter a while ago and found it to be an interesting read. I certainly recall the regex question he’s talking about and I remember stumbling upon that question myself back in the day. In the past StackOverflow did seem more like a community of developers who wanted to have fun and help eachother out. The dude makes some good points in his (now old and deleted) post.

However it has been archived and so I link to the archive here, mainly for my own future reference.

Why I no longer contribute to StackOverflow – Michael T. Richter

Building StackOverflow Reputation – One Answer a Day

So. I’m a little low in the reputation department of StackOverflow. I mean, my reputation is ok, but it’s not AWESOME. And I need my rep to be awesome because well it’s all in the name. So, I’ve come to the conclusion that I’m going to answer one StackOverflow question every day  (weekday that is… on the weekends I’ll probably be sleeping or eating pizza or sleeping. So stop expecting so much. Stop it.) How long am I going to do this? I have no idea. I plan on making this into a habitual thing.

So here is my StackOverflow reputation as of right now:
jeremysawesome StackOverflow RepRight here I’ve inserted the flair badge which should be kept up to date with my current rep points.

profile for jeremysawesome at Stack Overflow, Q&A for professional and enthusiast programmers

So we will see how the “answer a day” thing goes.

However, I’m going to try and make my answers as helpful as possible. I don’t want to just “give” an answer, I want to also explain it. I want to explain the problem and what it was I did to fix it. I want to link to relevant articles if necessary… I want my answers to serve as teaching material for anyone else who might eventually have the same question. Answering questions in this way I believe will benefit me as well as the person asking the question.

Why would answering questions benefit me? I think that it helps me personally to be able to communicate better. If I’m able to explain a problem and how I solved it to someone else, every single day, then it’ll help me level up my communication skills. If I’m able to clearly understand a problem, and how to solve it, it will help me with my debugging skills. If I don’t know how to solve a problem, then I will most likely be doing research into how to solve the problem. This, in turn, will help me to continue learning.

So let it begin, the quest to answer a StackOverflow question everyday is underway.