免责声明:我绝不是这方面的专家——以下答案为我解决问题的经验,但你的情况可能会有所不同。对于Windows代码页等更深层次的知识更熟悉的人可能会有更好的答案…
据我所了解,问题的核心在于git以utf8编写其输出,正如@lorek和@LeGEC在评论中指出的一样,但它却被命令提示符使用的Windows代码页所损坏。
您可以使用PowerShell或不使用PowerShell来重现此行为:
c:\repo> git status
On branch test_prüfung
nothing to commit, working tree clean
c:\repo> git branch
* test_pr<C3><BC>fung
c:\repo> git branch | more
* test_pr├╝fung
c:\repo> powershell "$x = git branch; write-host $x"
* test_pr├╝fung
c:\repo> powershell "git branch -D @(git branch | select-string -NotMatch master | ForEach-Object {$_.Line.Trim() })"
error: branch '* test_pr├╝fung' not found.
正在发生的是git将其输出编码为UTF8字节,然后shell使用不同的编码对其进行解码 - 类似于这样:
$branch = "test_prüfung";
$utf8 = [System.Text.Encoding]::Utf8.GetBytes($branch);
$mangled = [System.Text.Encoding]::GetEncoding(437).GetString($utf8);
write-host $mangled
其输出结果为:
test_pr├╝fung
在我的情况下,通过调用
chcp
来获取shell当前的代码页,确定了神奇的“编码437”。
C:\> chcp
Active code page: 437
根据 chcp 的文档,437 是表示美国的代码页。
对我而言,似乎可以解决这个问题的方法是使用代码页 65001(即 UTF8),那么你就会得到:
C:\repo> chcp 65001
Active code page: 65001
c:\repo> powershell "$x = git branch; write-host $x"
* test_prüfung
这个现在也正常工作了:
c:\repo> powershell "git branch -D @(git branch | select-string -NotMatch master | ForEach-Object {$_.Line.Trim() })"
Deleted branch test_prüfung (was 1e9bc02).
希望这能有所帮助...
(git branch | ...)
部分? - LeGECcmd.exe
在上世纪90年代出现,但UTF-8在2000年代变得流行起来... - aschipfl