Here’s a quick tip on howto remove Git Branches that were already merged (thus not necessary to keep around anymore) locally on Windows, using PowerShell. Assuming that you do have Git for Windows installed. Execute these commands on your own risk.

List all merged branches

You can list all merged Git Branches by running:

1
git branch --merged

Exclude current branch and “main” branch

Then we |-pipe it into the following, to exclude the currently selected branch (marked with an asterisk *) and the main branch. This regex filter query does not aim to be universally good. So if you have branches that include the term main in their name, those will be excluded aswell. I didn’t care to optimize, because I don’t name my branches in such a weird way.

1
Where-Object {$_ -notmatch "(^\*|main)"}

Remove each of the resulting branches

We remove the branches by utilizing forEach-Object to execute git branch -d

The whole command to remove all merged branches

WARNING: this is a destructive command!

1
git branch --merged | Where-Object {$_ -notmatch "(^\*|main)"} | forEach-Object { & git branch -d $($_.Trim()) }

Alternative: Remove ALL branches except the current branch and “main”

WARNING: this is a destructive command!

1
git branch | Where-Object {$_ -notmatch "(^\*|main)"} | forEach-Object { & git branch -D $($_.Trim()) }

Side note

Afterwards you might want to prune remote tracking branches, that don’t exist remote anymore. This is a fairly simple standard Git command:

1
git remote prune origin