我使用以下脚本删除git merged分支:
function Remove-MergedBranches
{
git branch -a --merged |
ForEach-Object { $_.Trim() } |
Where-Object {$_ -NotMatch "^\*"} |
Where-Object {-not ( $_ -Like "*master" )} |
Where-Object {-not ( $_ -Like "*develop" )} |
Where-Object {-not ( $_ -Like "*dev" )} |
% {$_.replace("/remotes","")} |
ForEach-Object { git branch -d $_ }
}
replace
无效。
分支可能是remotes/origin/tg-training-section
答案 0 :(得分:3)
你要替换/remotes
中不匹配的remotes/origin/tg-training-section
(它不是以斜杠开头),所以你可能想要省略斜杠:
# ....
% {$_.replace("remotes","")}
另一种方法是使用正则表达式/remotes
替换remotes
和-replace
:
# ....
% {$_ -replace '\/?remotes'}
我还会考虑使用\b\/?remotes
来确保您不会替换字符串中的remotes
。