How to Remove All the Git Branches Except the Master Locally

I am a marketer in love with tech and building communities around products. My passion is understanding how things work, what people think, and how organizations function.
Search for a command to run...

I am a marketer in love with tech and building communities around products. My passion is understanding how things work, what people think, and how organizations function.
Initially, I was thinking you'd delete the branch Pikachu from the repo, but it seems the command deletes all repos except master, right?
Yes, it removes all the local branches except the master.
PostHog recently published a newsletter titled "Collaboration sucks." It made some people angry, so they commented on it, and some shared it. Which means the post worked great, just not in the way you

This post is for everyone who ticks off any diversity boxes and is trying to find a safe and inclusive workspace. During the past few years, I’ve changed a few jobs and identified which red flags and green lights to look for. Perform a screening of t...

All of us have our regular morning routine. Habits and routines keep us grounded, make the day a bit more predictable, and help us prepare to face the rest of the day. However, if we want to keep moving forward, some habits need to get replaced with ...

I live in Serbia, and I've moved around the country multiple times. I went from the central part where I've lived for 19 years, then to the south for 5.5 years, and then moved north. I've been living in Novi Sad for 7 years now and I'm still as much ...

The marketing funnel or conversion/purchase funnel is a well-known marketing model that content marketers are familiar with and use in some shape or form. What I want to talk about in this post are multiple levels of the first part of the funnel and...

Note: I've moved all my projects to use the main instead of master branch.
I'm writing this post a note to my future forgetful self.
Today, I ran into a really stupid issue. I worked on updating a remote git repo, and I've already merged and deleted a branch on GitHub with this name, let's call it Pikachu for the sake of having an example.
So, after merging Pikachu to master, I realized that I needed to make another change to the file I've just pushed. "No biggie" I thought...
git checkout -b Pikachu
And then ....
fatal: A branch named 'Pikachu' already exists.
At first I was like ....

Until I realized that the branch I had locally was the one preventing me from generating one. I already merged and removed the same branch on GitHub. In my mind, every git pull on master should have removed the local branch, but for obvious reasons that would be a totally stupid thing to happen.
So, how do I clean up my local working repository so that I remove a gazillion stale branches I have locally? You need to get this into your terminal:
git branch | grep -v "master" | xargs git branch -D
In case you have a branch that contains the word "master" this won't work. Instead, you need to:
git branch | grep -ve " master$" | xargs git branch -D
That's it. Hope this note to my forgetful future self helps some of you as well 😉