Cleaning up all git merged local branches

Sam Smith's avatarPosted 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() }

References

2 comments

Leave a comment