Cleaning up all git merged local branches

Posted by

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() }



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s