在git中出现中文乱码的解决方案

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bingyu9875/article/details/88708860

在git中出现中文乱码的解决方案


一、解决方案

  • 在cygwin中,使用git add添加要提交的文件的时候,如果文件名是中文,会显示形如
    274\232\350\256\256\346\200\273\347\273\223.png 的乱码。

    解决方案:

    在bash提示符下输入:

     git config --global core.quotepath false
    

    core.quotepath设为false的话,就不会对0x80以上的字符进行quote。中文显示正常。

  • 在MsysGit中,使用git log显示提交的中文log乱码。

    解决方案:

    设置git gui的界面编码

     git config --global gui.encoding utf-8
    

    设置 commit log 提交时使用 utf-8 编码,可避免服务器上乱码,同时与linux上的提交保持一致!

     git config --global i18n.commitencoding utf-8
    

    使得在 $ git log 时将 utf-8 编码转换成 gbk 编码,解决Msys bash中git log 乱码。

     git config --global i18n.logoutputencoding gbk
    

    使得 git log 可以正常显示中文(配合i18n.logoutputencoding = gbk),在 /etc/profile 中添加:

     export LESSCHARSET=utf-8 
    

    此步骤为关键步骤。

  • 在MsysGit自带的bash中,使用ls命令查看中文文件名乱码。cygwin没有这个问题。

    解决方案:

    使用 ls --show-control-chars 命令来强制使用控制台字符编码显示文件名,即可查看中文文件名。
    为了方便使用,可以编辑 /etc/git-completion.bash ,新增一行 alias ls=”ls –show-control-chars”

二、参考

另外,还找到了一些相关的文章:

三、本人的gitconfig文件配置

sean@sean:~/work/mtk_open$ cat ~/.gitconfig 
[user]
	name = sean
	email = [email protected]
[color]
    ui = auto
	status = auto
[core]
	editor = vim
	excludesfile = /home/sean/.gitignore_global
[alias]
    st = status
    dt = difftool
    di = diff
    co = checkout
    ci = commit
	br = branch
    l0 = log --color --graph --decorate --pretty=oneline --abbrev-commit --date-order
    l1 = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
    l2 = log --all --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
	sta = stash
	chp = cherry-pick
    rem = remote
	dc = diff --cached
	de = describe --contains
	fp = format-patch -o /tmp/patch
	lg = log --color --graph --pretty=format:'%C(magenta)%h%Creset | %C(white)%s%Creset | %C(cyan)%cr%Creset | %C(ul normal)%an%Creset' --abbrev-commit
	ln = log --name-only
	logger = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --
	pr = pull --rebase
	re = rebase
	st = status
	unstage = reset HEAD --
	
[push]
	default = matching

[format]
	signoff = 1
	thread = 1

[grep]
	extendedRegexp = true
	lineNumber = true

[hooks]
	allownonascii = true

[push]
	default = upstream

[sendemail]
	aliasfiletype = mutt
	aliasesfile = ~/.mutt/aliases
    chainreplyto = false
	composeencoding = UTF-8
	confirm = always
	smtpencryption = ssl
	smtppass = lthgijktpyqlnpwh
	smtpserver = smtp.gmail.com
	smtpserverport = 465
	smtpuser = [email protected]
	suppressfrom = true
	# suppresscc = self

猜你喜欢

转载自blog.csdn.net/bingyu9875/article/details/88708860