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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 12:30:02  来源:igfitidea点击:

git log output encoding issues on Windows 10 command prompt

gitcharacter-encodinglocalecommand-prompt

提问by Marcin K?opotek

Problem

问题

How to make git logcommand output properly displayed on windows command prompt?

如何使git log命令输出正确显示在 Windows 命令提示符上?

Example

例子

git commands sequence leading to the problemAs you can see I can type diacritical characters properly but on git logthe output is somehow escaped. According to UTF-8encoding tablethe codes between angled brackets (<and >) from the output correspond to the previously typed git configparameters.

导致问题的 git 命令序列如您所见,我可以正确输入变音字符,但在git log输出中以某种方式进行了转义。根据UTF-8编码表,输出中尖括号 (<>)之间的代码对应于先前键入的git config参数。

I have tried to set LESSCHARSETenvironment variable to utf-8as sugested in one of the answers for similar issuebut then the output is garbled:

我试图在类似问题的答案之一中将LESSCHARSET环境变量设置utf-8为 sugested,但随后输出出现乱码:

git log output after setting LESSCHARSET=utf8

设置 LESSCHARSET=utf8 后的 git 日志输出

I know .git/configis encoded properly with utf-8as it's handled by gitkas expected.

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

Proper gitk output

正确的 gitk 输出

Here is localecommand 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的输出也是一样的:

enter image description here

在此处输入图片说明

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

然后享受结果。这是我的控制台上发生的事情(字体“Consolas”,没有chcp必要):

Windows console CMD

Windows 控制台 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 catyou profit from the aforementioned set LC_ALL=C.UTF-8.
  • 好吧,为了让 Windows 命令type(如(在控制台上显示文件))正常工作,您确实需要chcp 65001.
  • 如果您更喜欢来自 Git Bash 的命令,就像cat您从上述set LC_ALL=C.UTF-8.

Windows console CMD, part 2

Windows 控制台 CMD,第 2 部分



Update 2: How to make the changes permanent

更新 2:如何使更改永久化

As user blaine monosaid, create an environment variable LC_ALLand 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,无论是全局的还是您自己的用户配置文件(抱歉德国截图):

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.

下次打开命令处理器控制台 (cmd.exe) 时,您应该会在发出命令时看到变量值echo %LC_ALL%。在 PowerShell 中,您应该在发出$env:LC_ALL.

The simplest way to make the UTF-8 code page permanent ist to open regeeditand add a new value named Autorunof type string to section HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processorand assign it the value chcp 65001.

使 UTF-8 代码页永久化的最简单方法是打开regeedit并将名为Autorunstring 类型的新值添加到 section HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor并为其分配 value chcp 65001

Registration editor

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

此后,每次打开新的 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.txtor cat -encoding UTF8 file.txt(catbeing an alias for Get-Contentin PowerShell).

哦,顺便说一句:为了在 PowerShell 中正确显示 UTF-8 编码的文件,您可以使用Get-Content -encoding UTF8 file.txtor cat -encoding UTF8 file.txtcat作为Get-ContentPowerShell 中的别名)。

回答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_ALLwith the value C.UTF-8
  • or put $env:LC_ALL='C.UTF-8'in your $Profilefile
  • 创建一个以LC_ALL值命名的环境变量C.UTF-8
  • 或放入$env:LC_ALL='C.UTF-8'您的$Profile文件

回答by W.Perrin

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

git bashWIN10. 至于我,4个设置使外观符合我的预期。

  • envsetting. Add LC_ALL=C.UTF-8,LESSCHARSET=UTF-8to PATHglobally.

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

  • git bashsetting. Set Options-> Text-> Character setto utf-8. Or set localeand Character setboth to default. It is smart enough to choose the correct encoding.

  • env环境。添加LC_ALL=C.UTF-8,LESSCHARSET=UTF-8PATH全局。

  • git配置。git config --global i18n.logOutputEncoding utf-8.

  • git bash环境。设置Options-> Text-> Character setutf-8。或将localeCharacter 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 localewas cantaining all "C" letters, without UTF-8. To solve this, I uncommented en_US.UTF-8and ru_RU.UTF-8in /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-8ru_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)