How to Remove Remote Git Branches (snippet)

This snippet provides commands for removing local branches that have been deleted from the remote, using both Powershell and bash.

For both shells, run these commands:

git fetch
git remote prune origin

Any remote branches that you have locally but that have been removed from the remote are now marked as gone.

Next, remove / delete those branches. In Powershell:

git branch -v | select-string gone | ForEach-Object {$_.Line.Split(" ")[2]} | ForEach-Object {git branch -D $_}

In Bash:

git branch -v | grep gone | cut -f3 -d' ' | xargs git branch -D

This command deletes all removed branches, even if they are unmerged. Use -d instead to delete only merged branches.

Snippet from Jay Vilalta - 2024-09-10