Skip to content
0

🔧 Git 中文乱码完美解决方案

📋 一次配置,永久解决

Git命令行乱码 | 文件内容乱码 | 提交信息乱码 | 中文文件名显示异常

💡 适用场景:Windows 10/11 系统下使用 Git 进行版本控制时,中文文件内容在 git pullgit diffgit log 等操作中出现乱码,显示为 �? 等奇怪字符。


🔍 问题现象

常见乱码情况

文件内容乱码:执行 git diff 时,中文显示为 �?
文件名乱码git status 显示中文文件名为八进制编码
提交信息乱码git log 中的中文提交信息显示异常
合并冲突乱码:合并时中文注释变成乱码


🎯 问题原因分析

1️⃣ 字符编码不匹配

组件默认编码说明
Windows 系统GBK/GB2312中文Windows默认使用GBK编码
Git 内部UTF-8Git期望所有文本都是UTF-8编码
文本编辑器可能不一致不同编辑器可能使用不同编码

编码转换路径:

文件(UTF-8) → Git读取(GBK) → 显示(乱码)

2️⃣ Git 配置缺失

缺少以下关键配置参数:

  • core.quotepath - 文件路径引用显示配置
  • i18n.commitEncoding - 提交信息编码
  • i18n.logOutputEncoding - 日志输出编码
  • gui.encoding - GUI 界面编码

3️⃣ 缺少文件属性配置

没有 .gitattributes 文件来明确指定:

  • 文本文件的编码方式
  • 换行符处理规则(CRLF vs LF)
  • 二进制文件识别

✅ 完整解决方案

步骤 1️⃣:配置 Git 全局设置

核心配置命令

bash
# 禁用路径引用,使中文文件名正常显示
git config --global core.quotepath false

# 设置提交信息编码为 UTF-8
git config --global i18n.commitEncoding utf-8

# 设置日志输出编码为 UTF-8
git config --global i18n.logOutputEncoding utf-8

# 设置 GUI 界面编码为 UTF-8
git config --global gui.encoding utf-8

# 保持 Windows 换行符转换(建议保持)
git config --global core.autocrlf true

配置说明:

配置项作用必要性
core.quotepath false显示中文文件名而非八进制编码⭐⭐⭐⭐⭐
i18n.commitEncoding utf-8提交信息使用UTF-8编码⭐⭐⭐⭐⭐
i18n.logOutputEncoding utf-8日志输出使用UTF-8编码⭐⭐⭐⭐
gui.encoding utf-8GUI界面使用UTF-8编码⭐⭐⭐
core.autocrlf trueWindows下自动转换换行符⭐⭐⭐⭐

步骤 2️⃣:创建 .gitattributes 文件

在项目根目录创建 .gitattributes 文件:

bash
# .gitattributes 文件内容
# 设置所有文本文件使用 UTF-8 编码
* text=auto

# Markdown 文件
*.md text

# 配置文件
*.json text
*.yml text
*.yaml text

# JavaScript/TypeScript
*.js text
*.ts text

# Shell 脚本
*.sh text eol=lf
*.bat text eol=crlf
*.ps1 text eol=crlf

# 图片文件(二进制)
*.png binary
*.jpg binary
*.jpeg binary
*.gif binary
*.webp binary
*.svg binary

# 字体文件(二进制)
*.woff binary
*.woff2 binary
*.ttf binary
*.otf binary

作用说明:

  • ✅ 统一团队成员的文件编码和换行符处理方式
  • ✅ 避免Windows和Linux/Mac之间的换行符冲突
  • ✅ 明确区分文本文件和二进制文件

步骤 3️⃣:创建 .editorconfig 文件

统一编辑器配置,确保所有开发者使用相同的编码设置:

ini
# .editorconfig 文件内容
root = true

[*]
charset = utf-8
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false

[*.{bat,ps1}]
end_of_line = crlf

支持的编辑器:

  • ✅ VS Code
  • ✅ IntelliJ IDEA
  • ✅ WebStorm
  • ✅ Sublime Text
  • ✅ Atom
  • ✅ Vim/Neovim

步骤 4️⃣:配置 VS Code 编辑器

方法一:通过界面设置

  1. 打开设置(快捷键:Ctrl + ,
  2. 搜索 files.encoding,设置为 utf8
  3. 搜索 files.eol,设置为 \n (LF)
  4. 搜索 files.autoGuessEncoding,勾选启用

方法二:通过配置文件

编辑 settings.jsonCtrl + Shift + P → 输入 "settings.json"):

json
{
  "files.encoding": "utf8",
  "files.eol": "\n",
  "files.autoGuessEncoding": true,
  "files.insertFinalNewline": true,
  "files.trimTrailingWhitespace": true
}

查看当前文件编码:

  • 右下角状态栏显示当前文件的编码和换行符
  • 点击可以快速转换编码

🔧 进阶配置(可选)

配置 PowerShell 编码

编辑 PowerShell 配置文件:

powershell
# 在 PowerShell 中输入以下命令编辑配置文件
notepad $PROFILE

添加以下内容到配置文件:

powershell
# 设置控制台输出编码为 UTF-8
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$OutputEncoding = [System.Text.Encoding]::UTF8

# 设置 PowerShell 默认编码
$PSDefaultParameterValues['*:Encoding'] = 'utf8'

# 设置Git命令别名(可选)
Set-Alias -Name g -Value git

重启 PowerShell 生效

配置 Windows Terminal

Windows Terminal 配置文件示例:

打开设置(Ctrl + ,),在 settings.json 中添加:

json
{
  "profiles": {
    "defaults": {
      "font": {
        "face": "Cascadia Code",
        "size": 11
      },
      "startingDirectory": "%USERPROFILE%"
    }
  },
  "schemes": [
    {
      "name": "My Theme",
      "background": "#1E1E1E",
      "foreground": "#D4D4D4"
    }
  ]
}

推荐字体:

  • Cascadia Code
  • Consolas
  • Fira Code
  • JetBrains Mono

📝 验证配置是否生效

运行验证命令:

bash
# Linux/Mac 系统
git config --global --list | grep -E "core.quotepath|i18n|gui.encoding"

# Windows PowerShell
git config --global --list | Select-String "core.quotepath|i18n|gui.encoding"

预期输出结果:

bash
core.quotepath=false
i18n.commitencoding=utf-8
i18n.logoutputencoding=utf-8
gui.encoding=utf-8

如果看到以上输出,说明配置成功!


🎉 功能测试

配置完成后,测试以下Git操作:

1️⃣ 测试中文文件名显示

bash
git status

✅ 应该能正常显示中文文件名,而不是 \344\270\255\346\226\207 这样的八进制编码

2️⃣ 测试文件内容差异

bash
git diff 文件名.md

✅ 中文内容应该正常显示,不会出现 �? 乱码

3️⃣ 测试提交历史

bash
git log --oneline -10

✅ 提交信息中的中文应该正常显示

4️⃣ 测试远程同步

bash
git pull origin master

✅ 拉取时不会产生编码冲突


🚨 注意事项

⚠️ 重要提醒

1. 已存在的乱码文件需要手动修复

  • ❌ 这些配置不会自动修复已经产生的乱码
  • ✅ 只影响配置后的新操作
  • 🔧 已经乱码的文件需要手动用正确的中文替换

2. 团队协作建议

  • 📁 将 .gitattributes.editorconfig 提交到仓库
  • 👥 确保团队成员都配置相同的 Git 设置
  • 🛠️ 统一使用支持 EditorConfig 的编辑器

3. 编辑器设置

  • 📝 确保编辑器默认使用 UTF-8 编码保存文件
  • 🚫 避免使用 UTF-8 with BOM(会产生额外的字节标记)
  • ✅ 统一使用 LF 换行符(Windows用户注意)

4. 特殊情况处理

  • 如果配置后仍有乱码,检查文件本身的编码是否正确
  • 使用 file 命令(Linux/Mac)或编辑器查看文件实际编码
  • 必要时使用 iconv 或编辑器重新保存为 UTF-8

🛠️ 常见问题排查

❓ 配置后仍然乱码怎么办?

检查清单:

  • Git版本是否过旧?(建议 2.20+)
  • 文件本身是否是UTF-8编码?
  • 终端是否支持UTF-8显示?
  • VS Code 右下角显示的编码是 UTF-8 吗?
  • PowerShell 配置是否生效?(重启试试)

解决方法:

bash
# 1. 检查Git版本
git --version

# 2. 重新应用配置
git config --global --unset core.quotepath
git config --global core.quotepath false

# 3. 清除Git缓存
git rm -r --cached .
git add .
git commit -m "fix: 重新提交以应用编码配置"

❓ 如何转换已有文件的编码?

使用 VS Code:

  1. 打开文件
  2. 右下角点击当前编码(如 GBK)
  3. 选择 "通过编码保存"
  4. 选择 "UTF-8"

使用命令行(Linux/Mac):

bash
# 转换单个文件
iconv -f GBK -t UTF-8 原文件.md > 新文件.md

# 批量转换
find . -name "*.md" -exec sh -c 'iconv -f GBK -t UTF-8 "$1" > "$1.tmp" && mv "$1.tmp" "$1"' _ {} \;

📊 配置效果对比

配置前配置后
\344\270\255\346\226\207中文.md
�?�?�?这是中文内容
❌ 提交信息乱码✅ 正常显示中文
❌ diff 无法阅读✅ 清晰可读

📚 扩展阅读


✅ 配置完成!

现在你的 Git 仓库已经完美支持中文了!
建议将本文收藏,遇到问题时随时查阅。


📝 最后更新: 2025-10-19
💻 适用系统: Windows 10/11
🔧 Git 版本: 2.x+

如有疑问,欢迎在评论区留言交流!

最近更新