Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
解决 Git 在 windows 下中文乱码的问题

解决 Git 在 windows 下中文乱码的问题

原因

中文乱码的根源在于 windows 基于一些历史原因无法全面支持 utf-8 编码格式,并且也无法通过有效手段令其全面支持。

解决方案

  1. 安装

  2. 设置(以下需要修改的文件,均位于 git 安装目录下的 etc/ 目录中)

2.1. 让 Git 支持 utf-8 编码

在命令行下输入以下命令:

$ git config --global core.quotepath false  		# 显示 status 编码
$ git config --global gui.encoding utf-8			# 图形界面编码
$ git config --global i18n.commit.encoding utf-8	# 提交信息编码
$ git config --global i18n.logoutputencoding utf-8	# 输出 log 编码
$ export LESSCHARSET=utf-8
# 最后一条命令是因为 git log 默认使用 less 分页,所以需要 bash 对 less 命令进行 utf-8 编码

2.2. 让 ls 命令可以显示中文名称

修改 git-completion.bash 文件:

# 在文件末尾处添加一行
alias ls="ls --show-control-chars --color"

经过以上折腾之后,基本可以解决中文显示的问题。唯一的麻烦在于输入中文字符时会显示乱码,目前还没有完美的解决方案。

以下描述一个可用的临时方案:

  1. 前提条件:git commit 时,不用 -m 参数,也就是不在命令行下直接输入提交信息,而是敲回车,让 vim 来接管

  2. 进入 vim 后,按 i 键进入编辑模式,然后输入提交信息。(可多行)

  3. 输入完成后按 esc 退出编辑模式,然后输入 :wq,也就是写入+退出,即可。

  4. 如果进入 vim 后发现不能输入中文,那么先按 esc 退出编辑模式,然后输入::set termencoding=GBK,即可。(也可能是 GB2312,都试一下)

还好我们有 GUI

实在搞不定命令行的童鞋,请直接使用各种 GUI 工具吧!

  1. 使用 eclipse IDE的,可以安装 EGit 插件

  2. 不使用 IDE 的,可以搜索一个叫做 SmartGit 的 GUI 客户端

That's All!

@dongzhenye

This comment has been minimized.

Copy link

@dongzhenye dongzhenye commented Mar 9, 2015

请问对于Windows下创建的包含中文的Markdown文件,保存为何种编码才能让GitHub在Diff工具中不显示乱码呢?

@developbiao

This comment has been minimized.

Copy link

@developbiao developbiao commented Jun 2, 2015

终于解决了我的windows客户端下的问题,Thanks!

@mindcont

This comment has been minimized.

Copy link

@mindcont mindcont commented Oct 9, 2015

thanks !

@hanxiansen

This comment has been minimized.

Copy link

@hanxiansen hanxiansen commented Nov 24, 2015

解决了问题,非常感谢

@polunzh

This comment has been minimized.

Copy link

@polunzh polunzh commented Mar 25, 2016

解决了。赞一个!

@zfkingth

This comment has been minimized.

Copy link

@zfkingth zfkingth commented Apr 7, 2016

解决了问题,非常感谢

@Codyooo

This comment has been minimized.

Copy link

@Codyooo Codyooo commented Nov 30, 2016

感谢分享!git bash现在能正确显示中文了

@zhoulaiqing

This comment has been minimized.

Copy link

@zhoulaiqing zhoulaiqing commented Dec 26, 2016

现在我的Git Bash正常显示中文了,但为啥在powershell里面的git log还是不正常?

@szricky

This comment has been minimized.

Copy link

@szricky szricky commented Jan 1, 2017

it's useful~! many thanks!

@Kevin1207

This comment has been minimized.

Copy link

@Kevin1207 Kevin1207 commented Jan 19, 2017

赞!

@bruceqiq

This comment has been minimized.

Copy link

@bruceqiq bruceqiq commented Feb 27, 2017

我执行了这个命令,git log怎么编码还是有问题呢?望指导。

@wojiaxiaoguan

This comment has been minimized.

Copy link

@wojiaxiaoguan wojiaxiaoguan commented Mar 1, 2017

好棒 谢谢楼主

@zhangfanlong

This comment has been minimized.

Copy link

@zhangfanlong zhangfanlong commented Mar 6, 2017

Very very good.

@zhuozuoying

This comment has been minimized.

Copy link

@zhuozuoying zhuozuoying commented Sep 6, 2017

xiexie ha

@scwlwj

This comment has been minimized.

Copy link

@scwlwj scwlwj commented Sep 18, 2017

帮了大忙

@cuizhennan

This comment has been minimized.

Copy link

@cuizhennan cuizhennan commented Feb 8, 2018

不错

@dalerkd

This comment has been minimized.

Copy link

@dalerkd dalerkd commented Feb 27, 2018

很好的总结,解决我长久以来的担忧.

@gy0857478

This comment has been minimized.

Copy link

@gy0857478 gy0857478 commented Mar 13, 2018

好棒幫

@Mogeko

This comment has been minimized.

Copy link

@Mogeko Mogeko commented Mar 17, 2018

执行了楼主的命令后,git bash 中 git log 的中文显示正常了,但 cmd 中的还有问题。好在能用了。蟹蟹楼主!

@Fnine59

This comment has been minimized.

Copy link

@Fnine59 Fnine59 commented Mar 18, 2018

完美解决!太感谢了!

@harryhan618

This comment has been minimized.

Copy link

@harryhan618 harryhan618 commented Apr 7, 2018

第四行是git config --global i18n.logoutputencoding utf-8 还是git config --global i18n.logoutput.encoding utf-8 ?

@Wuaner

This comment has been minimized.

Copy link

@Wuaner Wuaner commented Apr 14, 2018

☺️☺️

@Seven-Steven

This comment has been minimized.

Copy link

@Seven-Steven Seven-Steven commented Apr 14, 2018

解决了,赞一个!

@codinggg

This comment has been minimized.

Copy link

@codinggg codinggg commented Aug 31, 2018

在git控制台,ls命令可以显示中文,但是在执行windows命令时显示的中文不会显示有解决方法吗?
错误显示如下:
$ taskkill /im ge.exe /t /f
▒▒▒▒: ▒▒Ч▒▒▒▒/ѡ▒▒ - 'C:/Program Files/Git/im'▒▒
▒▒▒▒ "TASKKILL /?" `▒▒▒˽▒▒÷▒▒▒

@WallenHan

This comment has been minimized.

Copy link

@WallenHan WallenHan commented Sep 30, 2018

老铁们 看看这个解决方案 楼主就差最后一句就解决完了所有问题
点我

@gogomarine

This comment has been minimized.

Copy link

@gogomarine gogomarine commented Oct 20, 2018

太棒了,一直在windows显示的是 unicode代码
这下解决了

@chinhuko

This comment has been minimized.

Copy link

@chinhuko chinhuko commented Mar 2, 2019

老铁们 看看这个解决方案 楼主就差最后一句就解决完了所有问题
点我

谢谢你,git log 中文乱码的问题解决了

@EKkoGGG

This comment has been minimized.

Copy link

@EKkoGGG EKkoGGG commented Apr 10, 2019

@WallenHan
感谢帮助

@suainam

This comment has been minimized.

Copy link

@suainam suainam commented Aug 28, 2019

多谢,解决了commit里面提交时候信息不是中文的问题

@nightire

This comment has been minimized.

Copy link
Owner Author

@nightire nightire commented Aug 29, 2019

我特么自己都不记得五年前我写过这玩意儿……

@Yu1993Yu

This comment has been minimized.

Copy link

@Yu1993Yu Yu1993Yu commented Sep 8, 2019

不知道作者有没有办法解决git客户端都会遇到中文乱码的问题了?

在sublime text 中可以用插件converttoUTF8插件转成GB2312编码格式解决中文乱码的问题。
但是在sublime merge中没办法直接导入converttoUTF8插件(如果直接复制到packages目录,会出现converttoUTF8插件,但是为灰色,无法使用)。
在Fork中也出现中文乱码,没有找到编码或者语言设置选项

目前只在博文中看到sourcetree有中文语言选项设置,因为使用sourcetree必须联网注册account,条件限制没办法使用。(不知道有没有办法绕过这个操作)

不知道作者有没有相关推荐办法解决这个问题?谢谢

@kid1412621

This comment has been minimized.

Copy link

@kid1412621 kid1412621 commented Nov 12, 2019

好奇怪,我 git status 没问题,git branch 就不行了

@mosjin

This comment has been minimized.

Copy link

@mosjin mosjin commented Dec 2, 2019

赞就一个字!

@xianghongai

This comment has been minimized.

Copy link

@xianghongai xianghongai commented Aug 2, 2020

2020-08-02-115830

[gui]
	encoding = utf-8
[i18n]
	commitencoding = utf-8
	logoutputencoding = utf-8
[svn]
	pathnameencoding = utf-8
[core]
	quotepath = false

Windows 10 Pro 2004 19041.264

Git Version 2.28.0.windows.1

@nocanstillbb

This comment has been minimized.

Copy link

@nocanstillbb nocanstillbb commented Aug 4, 2020

image
这个地方乱码有解吗, 分支名称一有中文符号马上凌乱了

@dictShuang

This comment has been minimized.

Copy link

@dictShuang dictShuang commented Aug 12, 2020

Git Version 2.28.0.windows.1 在windows10上乱码,目前网路上的各种方法都试过了,依然无法解决。
回退版本到2.25.0,乱码问题解决了。┓( ´∀` )┏。
本人决定先用老版本吧,等乱码问题修复了再升。

@han8gui

This comment has been minimized.

Copy link

@han8gui han8gui commented Sep 16, 2020

Git Version 2.28.0.windows.1 在windows10上乱码,目前网路上的各种方法都试过了,依然无法解决。
回退版本到2.25.0,乱码问题解决了。┓( ´∀` )┏。
本人决定先用老版本吧,等乱码问题修复了再升。

真棒,遇到同样的问题,解决了

@SWHL

This comment has been minimized.

Copy link

@SWHL SWHL commented Nov 5, 2020

@ dictShuang 我也是尝试了各种方法,都不行
最后,我更新了Git版本到2.29.2,竟然正常显示中文了

@scruel

This comment has been minimized.

Copy link

@scruel scruel commented Jan 7, 2021

@kid1412621 设置后 git log 等命令的不乱码了, 但是 git rebase -i 出现的交互窗口仍然是乱码

然后更新 git 版本到 2.30.0,可以了

@Jeff-Tian

This comment has been minimized.

Copy link

@Jeff-Tian Jeff-Tian commented May 2, 2021

老铁们 看看这个解决方案 楼主就差最后一句就解决完了所有问题
点我

不错!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment