kb0

yo! // software lead 🎮

hello, and welcome to my odd little chunk of the net!
日本語中級
(glad I could be here o7)

posts from @kb0 tagged #GUIDE

also:

> "a clean local is a happy remote."
- erwin schrödinger

yo cohort crew! it's a rare sunny day up here where I am, so it's time.

time for what?
coming in hot with a useful curated bash query, that is!

you could've googled this, but who cares? I have it saved in my Archive of Useful Code™.

have you ever wanted to clean up all 132 of those already-merged git branches you have in your uber-large project? I sure have, and so I set off on an adventure to find a command to do it. and I did! here it is:

git branch --merged | egrep -v "(^\*|main|dev)" | xargs git branch -d
Note: replace the egrep branch name expression after ^\*| with branches you want to keep. you can see I have main and dev ignored, since I don't want them deleted by this.

(if you're good, good! if you want to know more, keep reading!)
> so... what does the expression mean?
the pattern given to egrep is a regular expression (nice guide here). this specific one will match any of
  • a single asterisk, *
  • the word main
  • the word dev
see this regex101 example for an interactive demo. the asterisk is required to ignore the branch you're currently on . . . don't want to delete that out from under yourself.

as for the egrep command itself, egrep is just grep -E, for selecting expressions from a list of inputs. the -v flag means to select all expressions that don't match the supplied pattern regex, which means the above three will be ignored.

this means all branch names that are not those will get passed as an argument list to git branch -d, which naturally deletes the given branch(es).

and that's it! make sure to get outside and get some fresh air amidst all those wacky side projects you have . . . that I also have a lot of . . . I'll be sure to! 😎

> -kb0