问题
I have read several QAs to this problem, but none provided an answer. There is a workaround, which I state here again, but I want to understand and solve the problem.
Problem
The issue is that executing the command git diff reva revb | Out-File mypatch.patch
in powershell produces "garbage characters" in place of e.g. German umlauts (├ñ instead of ä).
Investigation
When I perform $Env:LESSCHARSET="utf8"
as suggested in some QAs, I do get correct output in the terminal, but once it is redirected to the file mypatch.patch
the umlauts (and other characters) are mangled. Even git --no-pager diff reva revb
results in correct output in the terminal. But as soon as you want to pipe that to a file, it is wrong. What you see is not what you get!
It seems to me that the input to Out-File
is already mangled and thus setting the -Encoding
argument does not change anything. I don't think Out-File
is to blame here. For instance, the command $mypatch = git diff reva revb
(even with --no-pager added before diff) results in a variable where e.g. Euro symbol or umlauts appear mangled (Ôé¼ instead of €) when that variable is printed to the terminal.
I tried powershell 5.1 and the open source powershell core 6.0.4 on Windows 10 (1709). I use git 2.18.0.windows.1. It works fine with the windows commandline (cmd), thus the simple workaround is to call from the powershell console:
Workaround
cmd /c "git diff reva revb > mypatch.patch"
Question
How does this work with powershell only?
回答1:
The problem seems to be caused by a wrong setting of [Console]::OutputEncoding
. If it is not set to UTF8, try setting it: [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
.
It does not matter if you then use $Env:LESSCHARSET
, respectively I believe it's not used anymore.
来源:https://stackoverflow.com/questions/52205297/the-output-of-git-diff-is-not-handled-correctly-in-powershell