> "a clean local is a happy remote."
- erwin schrödingeryo cohort crew! it's a rare sunny day up here where I am, so it's time.
time for what?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
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.
> 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
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