0

How I can delete all my local branches in VS 2017, except 'master' and current branch? without 'grep' command. I use with PowerShell in windows.

For some branches I did

git branch -D (git branch --list 2.68*).trim() and it works.

I want improve it.

Thanks

1 Answers1

0

Use PowerShell's Where-Object command to filter the output from git branch --list:

$allLocalBranches = git branch --list 
$branchesToDelete = $allLocalBranches |Where-Object {
    # the current branch is prefixed with `*`, so we can filter it out
    # by requiring branch names that start with whitespace
    # and then we also filter out the `master` branch
    $_ -match '^\s' -and $_.Trim() -ne 'master'
}

$branchesToDelete |ForEach-Object {
    # now we can go ahead and delete the remaining branches
    git branch -D $_.Trim()
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206