Delete multiple branches
September 1, 2022 · 1 min readLucian CiobanuOne of my favorite things about software development and delivery is versioning management. I never knew this was a thing and never thought I'd like it but the way git works with my imagination and stimulates me to think in branches and to grasp the concept that a folder can have visible files and certain versions of them or not, is just one of my favorite things.
However, git branches can get out of control, you work on stuff, push and merge it into the repo but you'll still have the branches locally. It took me some time to find out a solution but this is what I am using at the moment:
git branch | grep "pattern"
git branch | grep "pattern" | xargs git branch -d
What the above example does is that line 1 will look through all git branches but only show those who's name matches "pattern". The main project I am working on at the moment is called "supdev" so I have these six letters showing up very often in my task ids and this is the pattern I use to clean up my local branches.
Now, on line 3, we see "xargs" introduced which pushes each of the itesm from the list git branch | grep generates as argument to git branch -d.
A thing that bugged me for a very long time is that xargs wont work on windows in a command dashboard but it does work if we have git bash installed. I've done some research and it seems to have to do with the fact that xargs is specific to Linux so keep that in mind.
Hope this helped!
Cheers,
Lucian

Web Development & Software Delivery