Quite often you may face this trivial problem - remove merged local git branches. Usually we just go to google, type this query and follow the best StackOverflow answer.
The solution is:
git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d
But how it works? Actually there are 3 commands merged in a single pipeline:
git brach --merged- just returns a list of merged branches, including master and current branch.
egrep -v(^\*|master|dev)- filters branches to remove
devand current branch name(current branch name starts with
xargs git branch -d- simply takes the output of
egrepand uses it as arguments for
git branch -dcommand.
Of course the topic is much more complicated, and we can discuss regex, xargs and git infinitely, but briefly that’s how it works.