简体   繁体   中英

How to delete all merged local branches in Git with PowerShell

How can I iterate through my branches, filter out merged branches and delete them using Git for Windows in Powershell?

I have already attempted some research, but every answer I have found revolves around using bash specific commands, such as grep and xargs. Powershell does not have the same commands so these do not work for me.

However, from that research I have found that git for-each-ref --format '%(refname:short)' refs/heads can show me all local branches, while git branch -d will delete any merged branch. However, piping these two commands together ( git for-each-ref --format '%(refname:short)' refs/heads | git branch -d ) does not work as the output from the first command is not piped as I expected.

After some playing about, and further research into Powershell commands, I have found a solution!

While Powershell does not have an xargs command, it does have something similar called ForEach-Object . This command allows us to work on each line of the output from git for-each-ref . For this specific problem, the following line did the trick:

git for-each-ref --format '%(refname:short)' refs/heads | ForEach-Object {git branch $_ -d}

The curly braces after the ForEach-Object command contains the command you wish to run, while the $_ variable stands for each line of output from the piped command.

Hope this helps other newbie Powershell/Git users out there!

git branch --merged | Select-String -Pattern '^[^\\*].*' | ForEach-Object { git branch -d $_.ToString().Trim() }

Steps:

  1. List merged branches: git branch --merged

     feature/branch1 feature/branch2 * master
  2. Filter out those that starts with * (current branch): Select-String -Pattern '^[^\\*].*'

     feature/branch1 feature/branch2
  3. Delete branches ForEach-Object { git branch -d $_.ToString().Trim() }

Notice than in #3 I have removed some whitespace from the output: $_.ToString().Trim() . Otherwise we would get errors like:

error: branch '  feature/yourbranch' not found.

As a complement to Liero 's answer, you can use the following regex for excluding specific branches from the list of branches that are going to be deleted:

'^(?!.*(master|development)).*$'

Hence the full command becomes:

git branch --merged | Select-String -Pattern '^(?!.*(master|development)).*$' | ForEach-Object { git branch -d $_.ToString().Trim() }

PS : If your branches names contains non ascii characters you may have to switch Powershell's output encoding before running the command above, for instance:

[Console]::OutputEncoding = [System.Text.Encoding]::UTF8

You could also use this command:

git branch | %{ $_.Trim() } | ?{ $_ -ne 'master' } | %{ git branch -D $_ }

All credits to Andrei Fedotov

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM