解决 Powershell 里 Git 输出中文乱码的问题

三葉Leaves Author

问题

使用 git 命令时,有时候会遇到这样的乱码:

1
PS C:\Projects\metac\pcl-edu-platform> git reset --hard 842d1bba HEAD is now at 842d1bb chore: 浣跨敤鏇寸ǔ瀹氱殑鏂瑰紡杩涜鐢熶骇閮ㄧ讲

解决

先配置一下 git:

1
2
3
git config --global core.quotepath false
git config --global i18n.commitEncoding utf-8
git config --global i18n.logOutputEncoding utf-8

然后启动 powershell,然后在里面输入以下命令打开 Powershell 的配置文件:

1
notepad $PROFILE

然后写入:

1
2
3
4
5
6
7
8
# Force UTF-8 for native command output, e.g. Git for Windows
chcp.com 65001 > $null

$utf8 = [System.Text.UTF8Encoding]::new($false)

[Console]::OutputEncoding = $utf8
[Console]::InputEncoding = $utf8
$OutputEncoding = $utf8

保存后,关闭当前 PowerShell / Windows Terminal 标签页,重新打开

然后验证:

1
2
3
4
chcp  
[Console]::OutputEncoding.WebName
[Console]::InputEncoding.WebName
$OutputEncoding.WebName

期望输出可能是:

1
2
3
4
Active code page: 65001
utf-8
utf-8
utf-8
  • 标题: 解决 Powershell 里 Git 输出中文乱码的问题
  • 作者: 三葉Leaves
  • 创建于 : 2026-05-07 00:00:00
  • 更新于 : 2026-05-07 11:27:27
  • 链接: https://blog.oksanye.com/9382f6706e13/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论
目录
解决 Powershell 里 Git 输出中文乱码的问题