我有以下用于删除git合并分支的脚本:
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
最佳答案
您不希望在/remotes
中替换不匹配的remotes/origin/tg-training-section
(它不以斜杠开头),因此您可能想省略斜杠:
# ....
% {$_.replace("remotes","")}
另一种方法是使用带有正则表达式的
/remotes
替换remotes
和-replace
:# ....
% {$_ -replace '\/?remotes'}
我还考虑甚至使用
\b\/?remotes
来确保您不替换字符串中的remotes
。关于powershell - 替换管道中的文字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39766434/