COOKIES! This blog uses cookies!
I am completely out of control of cookies here, otherwise I would have disabled them (it is controlled by the platform).
If you don't like cookies and being tracked please leave this blog immediately.

Friday, 24 May 2019

Remove merged local branches (git)

This command executed in in project directory with bash will delete all local branches which were already merged into master:
git branch --merged | egrep -v "(^\*|master)" | xargs git branch -d

Explanation:
git branch --merged - list all merged branches
egrep -v "(^\*|master)" - grep "master" from provided lines one by one (piped from git branch above)
xargs git branch -d - delete filtered branches (piped from egrep above)

Same sort of command in PowerShell:

git branch --merged main | ForEach-Object { if ($_  -ne '* main' -and $_ -ne '* master') { git branch -d $_.trim() } }