在 Windows10 CLI 终端上 git 日志输出编码问题

问题

如何在 Windows CLI 终端上正确显示 git log命令输出?

例子

git commands sequence leading to the problem 正如您所看到的,我可以正确地输入发音字符,但是在 git log上输出以某种方式转义。根据 UTF-8编码表,输出中角括号(<>)之间的代码对应于之前输入的 git config参数。

我曾试图按照 类似问题的答案之一中的建议,将 LESSCHARSET环境变量设置为 utf-8,但随后输出出现了混乱:

git log output after setting LESSCHARSET=utf8

我知道 .git/config是用 utf-8正确编码的,因为它是由 gitk按照预期处理的。

Proper gitk output

如果需要,这里是 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=

编辑:

基特狂欢的输出也是相同的:

enter image description here

所以我认为这个问题与 shell 无关,并且与 Git 或其配置本身有关。

25929 次浏览

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):

Windows console CMD


Update:

  • Well, in order for Windows commands like type (display file on console) to work correctly, you do need chcp 65001.
  • And if you prefer commands from Git Bash like cat you profit from the aforementioned set LC_ALL=C.UTF-8.

Windows console CMD, part 2


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):

Create environment variable

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_MACHINE\SOFTWARE\Microsoft\Command Processor and assign it the value chcp 65001.

Registration editor

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).

If anyone is interested in the PowerShell equivalent of set LC_ALL=C.UTF-8, that is:

$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 value C.UTF-8
  • or put $env:LC_ALL='C.UTF-8' in your $Profile file

I had to use the windows powershell command prompt instead of the default one (Windowkey + X)

I use git bash on WIN10. As for me, 4 settings make the appearance as my expectation.

  • env setting. Add LC_ALL=C.UTF-8,LESSCHARSET=UTF-8 to PATH globally.

  • git config. git config --global i18n.logOutputEncoding utf-8.

  • git bash setting. Set Options-> Text-> Character set to utf-8. Or set locale and Character set both to default. It is smart enough to choose the correct encoding.

Done.

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.

I am using Git via Powershell Core v7.0.3 inside Windows Terminal on Windows 10.

I have been browsing through answers and tried many of them. The solutions that worked for me were:

These solutions both work separately. I chose to use the Git command as the problem seems to be related to Git, and Powershell profile stays cleaner.

git config --global core.pager 'less --raw-control-chars'
 

#Or


git config --global core.pager 'more'