You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows11下Mercurial迁移Git后,Git无法正确识别含变音符号的文件

问题

在Windows 11的Git Bash中执行Mercurial仓库迁移至Git的操作:

MINGW64$ ls
hg-repo/ git-repo/
MINGW64$ cd git-repo
MINGW64$ git init
MINGW64$ ~/fast-export/hg-fast-export.sh -r ../hg-repo/ --force -A ../hg-repo/authors.txt -M main

迁移完成后执行git checkout main,正常情况下工作区应无变更,但实际得到如下状态:

MINGW64$ git status
On branch main
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
    deleted:    Folder1/grünes-Ding.png
Untracked files:
(use "git add <file>..." to include in what will be committed)
    Änderungen/
    Folder1/grünes-Ding.png

尝试恢复文件时出现错误:

MINGW64$ git restore Folder1/grünes-Ding.png
error: pathspec 'Folder1/grünes-Ding.png' did not match any file(s) known to git

推测问题源于Git对含变音符号(如ü、Ä)的文件/目录编码识别不一致,导致Git Bash显示的路径与Git内部存储的路径不匹配。当前Git配置及系统locale如下:

MINGW64$ git config -l
diff.astextplain.textconv=astextplain
http.sslbackend=openssl
http.sslcainfo=C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt
core.autocrlf=input
core.fscache=true
core.symlinks=false
pull.rebase=false
init.defaultbranch=main
difftool.sourcetree.cmd=''
mergetool.sourcetree.cmd=''
mergetool.sourcetree.trustexitcode=true
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
core.quotepath=false
core.fsmonitor=true
i18n.logoutputencoding=UTF-8
MINGW64$ locale
LANG=en_GB.UTF-8
LC_CTYPE="en_GB.UTF-8"
LC_NUMERIC="en_GB.UTF-8"
LC_TIME="en_GB.UTF-8"
LC_COLLATE="en_GB.UTF-8"
LC_MONETARY="en_GB.UTF-8"
LC_MESSAGES="en_GB.UTF-8"
LC_ALL=

需要解决的是:如何配置Git以正确处理含变音符号的文件和文件夹?

解决方案

1. 启用文件名Unicode预组合支持

Windows NTFS文件系统使用Unicode归一化形式C(NFC)存储文件名,而Git在部分环境下可能默认使用归一化形式D(NFD),导致编码不匹配。通过以下命令开启core.precomposeunicode配置:

git config --global core.precomposeunicode true

如果仅针对当前仓库生效,去掉--global参数即可。

2. 验证并调整大小写敏感性

Windows文件系统默认不区分大小写,当前配置已设置core.ignorecase=true,符合Windows环境,但需确保迁移过程中未出现大小写不一致的路径。

3. 禁用文件系统监控(可选)

若core.fsmonitor导致编码识别异常,可临时禁用该功能测试:

git config --local core.fsmonitor false

之后重新执行git checkout main,查看状态是否恢复正常。

4. 重新执行迁移(若上述配置无效)

如果配置调整后问题仍存在,需确保hg-fast-export.sh使用UTF-8编码处理文件名。可在执行迁移前,在Git Bash中明确设置环境变量:

export LC_ALL=en_GB.UTF-8
export LANG=en_GB.UTF-8

然后重新运行迁移命令,确保文件名编码在迁移过程中被正确转换。

5. 手动修复路径(临时方案)

若上述方法均无效,可通过Git的原始路径(内部存储的编码形式)操作:

  • 使用git ls-files --stage查看Git内部存储的文件名原始编码
  • 复制对应路径,用引号包裹后执行恢复或添加操作,例如:
    git restore "Folder1/grünes-Ding.png"
    

内容的提问来源于stack exchange,提问作者marli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 00:05:25