Skip to content

Git: How to delete all local branches except master

twitter logo github logo ・1 min read  

Checkout to master branch.

The command to delete all branches except master is:
git branch | grep -v "master" | xargs git branch -D

To use the same command in Windows we have to install some utilities

Or, we can use PowerShell command that do the same thing that above command do:
git branch | %{ $_.Trim() } | ?{ $_ -ne 'master' } | %{ git branch -D $_ }
git branch -D @(git branch | select-string -NotMatch "master" | Foreach {$_.Line.Trim()})
Alt text of image

twitter logo DISCUSS (3)
markdown guide

Ha! Sweet. Super simple and super efficient. Thanks for sharing.


I did noticed on the first powershell one liner, I did noticed that one of the response lines was:

"error: branch '* master' not found."



I've just checked it again in PowerShell and everything works. I made a quick check like:
git init
git branch new
git branch new2
git branch new3
git branch new4
git branch | grep -v "master" | xargs git branch -D

Just to confirm, are you sure that you installed Gow utilities, input the command correctly and that you are in master branch?

Classic DEV Post from Mar 29 '19

A dev's guide to meditation - part 1

Why meditation might help you code

Andrei Fedotov profile image
I write here some notes