I came across a useful script I wanted to share today. This deletes all local git branches that have been merged, and are not “main” or “master”. This is challenging in Visual Studio today, deleting the branches one by one is slow and manual.
In PowerShell:
git branch --merged | %{$_.trim()} | ?{$_ -notmatch 'master' -and $_ -notmatch 'main'} | %{git branch -d $_.trim()}
An alternative PowerShell option (updated 30-May-2022):
git branch | Select-String -NotMatch -Pattern "master" | %{ git branch -D $_.ToString().Trim() }
Nice one-liner!
I’ve been using a `Clear-DeletedBranches` function in my PowerShell profile that prompts to show what branches would be deleted before deleting them.
Here’s the gist for others if interested: https://gist.github.com/adamrushuk/45ed2dc59dbb7c99fcac0e7cb99fa95f
LikeLike