Windows 10 CLI 终端上的 git log 输出编码问题 [英] git log output encoding issues on Windows 10 CLI terminal
问题描述
问题
如何让 git log
命令输出正确显示在 Windows CLI 终端上?
示例
如您所见,我可以正确输入变音字符,但在 git log
上,输出以某种方式进行了转义.根据
我知道 .git/config
使用 utf-8
正确编码,因为它按预期由 gitk
处理.
如果需要,这里是 locale
命令输出
LANG=LC_CTYPE=C.UTF-8"LC_NUMERIC=C.UTF-8"LC_TIME=C.UTF-8"LC_COLLATE=C.UTF-8"LC_MONETARY=C.UTF-8"LC_MESSAGES=C.UTF-8"LC_ALL=
在纯 git-bash 中的输出也是一样的:
所以我认为这个问题与 shell 无关,并且与 Git 或其配置本身有关.
好吧,我尝试了一下,发现 Windows Git 命令实际上需要像 LC_ALL
这样的 UNIX 变量来显示波兰语(或其他 UTF-8 字符)正确.试试这个命令:
set LC_ALL=C.UTF-8
然后享受结果.这是我的控制台上发生的事情(字体Consolas",不需要 chcp
):
更新:
- 好吧,为了使
type
(在控制台上显示文件)等 Windows 命令正常工作,您确实需要chcp 65001
. - 如果你更喜欢来自 Git Bash 的命令,比如
cat
,你可以从前面提到的set LC_ALL=C.UTF-8
中获益.
更新 2:如何使更改永久化
正如用户 mono blaine 所说,创建一个环境变量 LC_ALL
并为其分配值 C.UTF-8
,全局或用于仅限您自己的用户个人资料(对不起,德语截图):
下次打开命令处理器控制台 (cmd.exe) 时,您应该会在发出命令 echo %LC_ALL%
时看到变量值.在 PowerShell 中,您应该在发出 $env:LC_ALL
时看到它.
使 UTF-8 代码页永久化的最简单方法是打开 regeedit
并将名为 Autorun
的字符串类型的新值添加到部分HKEY_LOCAL_MACHINESOFTWAREMicrosoftCommand Processor
并为其分配值 chcp 65001
.
此后,每次打开新的 cmd.exe 控制台时都会执行此命令.您甚至可以在新窗口中看到它的输出:Aktive Codepage: 65001".(或在您各自的语言中类似).
哦,顺便说一句:为了在 PowerShell 中正确显示 UTF-8 编码的文件,您可以使用 Get-Content -encoding UTF8 file.txt
或 cat -encoding UTF8 文件.txt
(cat
是 PowerShell 中 Get-Content
的别名).
Problem
How to make git log
command output properly displayed on Windows CLI terminal?
Example
As you can see I can type diacritical characters properly but on git log
the output is somehow escaped. According to UTF-8
encoding table the codes between angled brackets (<
and >
) from the output correspond to the previously typed git config
parameters.
I have tried to set LESSCHARSET
environment variable to utf-8
as sugested in one of the answers for similar issue but then the output is garbled:
I know .git/config
is encoded properly with utf-8
as it's handled by gitk
as expected.
Here is locale
command output if necessary
LANG=
LC_CTYPE="C.UTF-8"
LC_NUMERIC="C.UTF-8"
LC_TIME="C.UTF-8"
LC_COLLATE="C.UTF-8"
LC_MONETARY="C.UTF-8"
LC_MESSAGES="C.UTF-8"
LC_ALL=
EDIT:
The output is the same also in pure git-bash:
so I believe the problem is shell independent and relates to Git or its configuration itself.
Okay, I experimented a bit and found out that Windows Git commands actually need UNIX variables like LC_ALL
in order to display Polish (or other UTF-8 characters) correctly. Just try this command:
set LC_ALL=C.UTF-8
Then enjoy the result. Here is what happened on my console (font "Consolas", no chcp
necessary):
Update:
- Well, in order for Windows commands like
type
(display file on console) to work correctly, you do needchcp 65001
. - And if you prefer commands from Git Bash like
cat
you profit from the aforementionedset LC_ALL=C.UTF-8
.
Update 2: How to make the changes permanent
As user mono blaine said, create an environment variable LC_ALL
and assign it the value C.UTF-8
, either globally or for your own user profile only (sorry for the German screenshot):
Next time you open a command processor console (cmd.exe) you should see the variable value when issuing the command echo %LC_ALL%
. In PowerShell you should see it when issuing $env:LC_ALL
.
The simplest way to make the UTF-8 code page permanent ist to open regeedit
and add a new value named Autorun
of type string to section
HKEY_LOCAL_MACHINESOFTWAREMicrosoftCommand Processor
and assign it the value chcp 65001
.
Henceforth, this command will be executed each time you open a new cmd.exe console. You even see its output in the new window: "Aktive Codepage: 65001." (or similar in your respective language).
Oh, by the way: In order to display a UTF-8 encoded file correctly in PowerShell you can use Get-Content -encoding UTF8 file.txt
or cat -encoding UTF8 file.txt
(cat
being an alias for Get-Content
in PowerShell).
这篇关于Windows 10 CLI 终端上的 git log 输出编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!