Skip to content

Instantly share code, notes, and snippets.

@zfkingth
Last active June 18, 2018 02:56
Show Gist options
  • Save zfkingth/5868240 to your computer and use it in GitHub Desktop.
Save zfkingth/5868240 to your computer and use it in GitHub Desktop.
Windows中Git 的配置,external diff merge工具配置 p4merge,并解决中文乱码
虽然git内置了diff和merge工具,但是很不好用,尤其使用中文时,容易出现乱码,在找了众多资料后,成功在windows环境下配置git
的diff和merge。
这里以p4merge工具为例子,
1.首先安装p4merge,可以到http://www.perforce.com/downloads/Perforce-Software-Version-Management/complete_list/20-20#10下载
Being the installation path "C:Program Files\Perforce\p4merge.exe",下面的命令会用到这个绝对路径
2.确保安装了git
3.在'c:/Program Files/Git/mingw64/bin目录中建立文件名称为:p4-diff-wrapper.sh 的文件,其内容为:
#!/bin/sh
# diff is called by git with 7 parameters:
# path old-file old-hex old-mode new-file new-hex new-mode
oldfile="$2"
newfile="$5"
if
[ "$oldfile" != "/dev/null" -a "$newfile" != "/dev/null" ]
then
#echo
#echo "split ************"
#echo "$oldfile"
#echo "$newfile"
'c:/Program Files/Perforce/p4merge.exe' "$2" "$5"
fi
######分隔符###########
4.执行下面所有命令
username为你登录windows的用户名)修改如下:
#modify,......
# 全局提交用户名与邮箱
git config --global user.name "zfking"
git config --global user.email zfking@gmail.com
#执行以下脚本(再别人的基础上修改)
# 中文编码支持
echo "export LESSCHARSET=utf-8" > $HOME/.profile
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
# 全局编辑器,提交时将COMMIT_EDITMSG编码转换成UTF-8可避免乱码
git config --global core.editor notepad2
# 差异工具配置
git config --global diff.external "'c:/Program Files/Git/mingw64/bin/p4-diff-wrapper.sh'"
git config --global diff.tool p4merge
git config --global difftool.p4merge.cmd 'p4merge.exe $LOCAL $REMOTE'
git config --global difftool.prompt false
# 合并工具配置
git config --global merge.tool p4merge
git config --global mergetool.p4merge.path 'C:\Program Files\Perforce\p4merge.exe'
git config --global mergetool.prompt false
git config --global --unset mergetool.p4merge.cmd
# 别名设置
git config --global alias.dt difftool
git config --global alias.mt mergetool
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.st status
git config --global alias.ci commit
# 取消 $ git gui 的中文界面,改用英文界面更易懂
if [ -f "/share/git-gui/lib/msgs/zh_cn.msg" ]; then
rm /share/git-gui/lib/msgs/zh_cn.msg
fi
#执行完成后,再git bash点右键,Options->Text面板中Local选择 zh_cn,characterset选择UTF-8
#这样就配置好了,注意第一次运行git diff是需要在p4merge的Edit->Preferences中设置Character Encoding,最好设置成utf-8
@anpbszzz
Copy link

不行啊

@zfkingth
Copy link
Author

现在可以了。

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