DEV Community

Discussion on: Git: How to delete all local branches except master

Collapse
 
ap3rus profile image
Vladislav Nagornyi

It's the PS script which gives this error, as the git branch prefixes selected branch with asterisk:

❯ git branch |`
  %{ $_.Trim() } |`
  ?{ $_ -ne 'master' } |`
  %{ git branch -D $_ }
Enter fullscreen mode Exit fullscreen mode
Deleted branch <branch1> (was <commit1>).
error: branch '* master' not found.
Enter fullscreen mode Exit fullscreen mode

Could be addressed e.g. by adding check for * master:

git branch |`
  %{ $_.Trim() } |`
  ?{ $_ -ne 'master' -and $_ -ne '* master' } |`
  %{ git branch -D $_ }
Enter fullscreen mode Exit fullscreen mode