Powershell: Delete all local branches except master

use carefully

Assuming your local git repo is on master, you can delete all local branches except master using this one-line powershell script:

git branch | foreach { $_.split( "`n" ) } | foreach { if ($_ -ne '* master' ) { git branch -D $_.replace(' ', '') } }

I strongly recommend doing first a dry-run to see which branches will be deleted:

git branch | foreach { $_.split( "`n" ) } | foreach { if ($_ -ne '* master' ) { write $_ } }

After I wrote the code above, I found this one: https://dev.to/koscheyscrag/git-how-to-delete-all-branches-except-master-2pi0 which seems nicer :)