Windows 10 命令提示符下的 git log 输出编码问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41139067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
git log output encoding issues on Windows 10 command prompt
提问by Marcin K?opotek
Problem
问题
How to make git log
command output properly displayed on windows command prompt?
如何使git log
命令输出正确显示在 Windows 命令提示符上?
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 tablethe codes between angled brackets (<
and >
) from the output correspond to the previously typed git config
parameters.
如您所见,我可以正确输入变音字符,但在
git log
输出中以某种方式进行了转义。根据UTF-8
编码表,输出中尖括号 (<
和>
)之间的代码对应于先前键入的git config
参数。
I have tried to set LESSCHARSET
environment variable to utf-8
as sugested in one of the answers for similar issuebut then the output is garbled:
我试图在类似问题的答案之一中将LESSCHARSET
环境变量设置utf-8
为 sugested,但随后输出出现乱码:
I know .git/config
is encoded properly with utf-8
as it's handled by gitk
as expected.
我知道.git/config
编码正确,utf-8
因为它gitk
按预期处理。
Here is locale
command output if necessary
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=
EDIT:
编辑:
The output is the same also in pure git-bash:
纯git-bash的输出也是一样的:
so I believe the problem is shell independent and relates to Git or its configuration itself.
所以我相信这个问题是独立于 shell 的,并且与 Git 或其配置本身有关。
回答by kriegaex
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:
好吧,我尝试了一下,发现 Windows Git 命令实际上需要 UNIX 变量,例如LC_ALL
为了正确显示波兰语(或其他 UTF-8 字符)。试试这个命令:
set LC_ALL=C.UTF-8
Then enjoy the result. Here is what happened on my console (font "Consolas", no chcp
necessary):
然后享受结果。这是我的控制台上发生的事情(字体“Consolas”,没有chcp
必要):
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
.
- 好吧,为了让 Windows 命令
type
(如(在控制台上显示文件))正常工作,您确实需要chcp 65001
. - 如果您更喜欢来自 Git Bash 的命令,就像
cat
您从上述set LC_ALL=C.UTF-8
.
Update 2: How to make the changes permanent
更新 2:如何使更改永久化
As user blaine monosaid, 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):
正如用户blaine mono所说,创建一个环境变量LC_ALL
并为其分配值C.UTF-8
,无论是全局的还是您自己的用户配置文件(抱歉德国截图):
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
.
下次打开命令处理器控制台 (cmd.exe) 时,您应该会在发出命令时看到变量值echo %LC_ALL%
。在 PowerShell 中,您应该在发出$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_MACHINE\SOFTWARE\Microsoft\Command Processor
and assign it the value chcp 65001
.
使 UTF-8 代码页永久化的最简单方法是打开regeedit
并将名为Autorun
string 类型的新值添加到 section
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor
并为其分配 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).
此后,每次打开新的 cmd.exe 控制台时都会执行此命令。您甚至可以在新窗口中看到它的输出:“Aktive Codepage: 65001”。(或在您各自的语言中类似)。
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).
哦,顺便说一句:为了在 PowerShell 中正确显示 UTF-8 编码的文件,您可以使用Get-Content -encoding UTF8 file.txt
or cat -encoding UTF8 file.txt
(cat
作为Get-Content
PowerShell 中的别名)。
回答by mono blaine
If anyone is interested in the PowerShell equivalent of set LC_ALL=C.UTF-8
, that is:
如果有人对 PowerShell 等价物感兴趣set LC_ALL=C.UTF-8
,那就是:
$env:LC_ALL='C.UTF-8'
However this works only for the current session. To make it permanent, two possibilities:
但是,这仅适用于当前会话。要使其永久化,有两种可能性:
- create an environment variable named
LC_ALL
with the valueC.UTF-8
- or put
$env:LC_ALL='C.UTF-8'
in your$Profile
file
- 创建一个以
LC_ALL
值命名的环境变量C.UTF-8
- 或放入
$env:LC_ALL='C.UTF-8'
您的$Profile
文件
回答by W.Perrin
I use git bash
on WIN10
. As for me, 4 settings make the appearance as my expectation.
我git bash
在WIN10
. 至于我,4个设置使外观符合我的预期。
env
setting. AddLC_ALL=C.UTF-8
,LESSCHARSET=UTF-8
toPATH
globally.git
config.git config --global i18n.logOutputEncoding utf-8
.git bash
setting. SetOptions-> Text-> Character set
toutf-8
. Or setlocale
andCharacter set
both todefault
. It is smart enough to choose the correctencoding
.
env
环境。添加LC_ALL=C.UTF-8
,LESSCHARSET=UTF-8
到PATH
全局。git
配置。git config --global i18n.logOutputEncoding utf-8
.git bash
环境。设置Options-> Text-> Character set
为utf-8
。或将locale
和Character set
都设置为default
。选择正确的encoding
.
Done.
完毕。
回答by Ashark
I had such problem on Linux. And the problem was that I did not generated locales. So my output of locale
was cantaining all "C" letters, without UTF-8.
To solve this, I uncommented en_US.UTF-8
and ru_RU.UTF-8
in /etc/locale.gen. Then I run localectl set-locale LANG=ru_RU.UTF-8 and rebooted. And relogined to the system. After that ciryllic was displayed normally.
我在 Linux 上遇到过这样的问题。问题是我没有生成语言环境。所以我的输出locale
是包含所有“C”字母,没有 UTF-8。为了解决这个问题,我的注释去掉,en_US.UTF-8
并ru_RU.UTF-8
在文件/etc/locale.gen。然后我运行 localectl set-locale LANG=ru_RU.UTF-8 并重新启动。并重新登录系统。之后,西里尔文显示正常。
回答by john ktejik
I had to use the windows powershell command prompt instead of the default one (Windowkey + X)
我不得不使用 windows powershell 命令提示符而不是默认的(Windowkey + X)