How to delete all local branches except master in git
The idea is to list the local branches using git branch
, filter the results and apply git branch -d
for each line.
Linux/MacOS
git branch
lists all available local branches. With grep -v
the master
branch is filtered out. Then each line is passed to git branch -D
using xargs
.
Windows
The same idea here, but with different tools. Instead of grep -v
we’ll use findstr /v
. Unfortunately xargs
has no alternative, the only way is to use a for
loop.
PowerShell
Here we output the branches without leading spaces and in quotes(--format '%(refname:lstrip=2)'
). The grep -v
alternative in PowerShell
is Where-Object
with the -ne
(not equals) operator. Then for each line git branch -d
is applied with ForEach-Object
, which is the xargs
alternative.
Comments