Previously, I wrote about how to delete git branches that have been merged and no longer exist on the remote using git bash. Using git bash worked just fine for this. However, my normal shell is PowerShell and I want to stay in PowerShell.

In this post, we will look at how to use PowerShell instead to delete your local git branches that have been merged and no longer exist on the remote.

  1. Open PowerShell and navigate to your git repository that you want to clean up

  2. Make sure you are on the main branch

    git checkout main
    
  3. Fetch the latest from the git

    git pull --prune
    
  4. See the list of local git branches

    git branch
    
  5. Delete all local branches that have been merged to main branch

    
    git branch -vv | where {$_ -match '\[origin/.*: gone\]'} | foreach { git branch -d $_.split(" ", [StringSplitOptions]'RemoveEmptyEntries')[0]}
    

    Sometimes it may give you an error that the the branch is not fully merged and you will need to change the -d to a -D

  6. See the list of local git branches that remain

    git branch