如何解决:在 Windows 上使用 Git 时出现错误:“无效路径”。
由 Mux 主办的 DEV 全球展示挑战赛:展示你的项目!
我尝试从 Gitbucket 客户端克隆一个仓库时,电脑上出现了这个错误。
git -c filter.lfs.smudge= -c filter.lfs.required=false -c diff.mnemonicprefix=false -c core.quotepath=false --no-optional-locks clone --branch master https://JumboDaniel@bitbucket.org/fedacash-dev/fedhagap-web-ui.git C:\Users\hp\Documents\HTML\Jobs\Fedha
Cloning into 'C:\Users\hp\Documents\HTML\Jobs\Fedha\...
error: invalid path 'src/Assets/fonts/TTF/THICCCBOI-Black.ttf:Zone.Identifier'
fatal: unable to checkout working tree
warning: Clone succeeded, but checkout failed.
You can inspect what was checked out with 'git status'
and retry with 'git restore --source=HEAD :/'
我尝试过各种方法来解决这个问题;我甚至尝试从 Gitbucket 仓库中删除“THICCCBOI-Black.ttf”文件,但这并没有解决问题。
经过几个小时的网上搜索,我终于找到了错误的原因:
在 Windows 系统上,使用 Git 克隆仓库会因为“路径无效”而失败,这是因为 Windows 操作系统会保留一些文件名;因此,某个文件名在 Linux 或 Mac 系统(就我的情况而言)中可能是合法的,但在 Windows 系统中却是非法的。
**
解决方案:
**
虽然这可能可以解决这个问题,但应该谨慎,因为 Windows 客户端的默认值设置为 core.protectNTFS=true,以解决此处描述的潜在安全问题 CVE-2019-1353。
根据文件名,您可以配置 Git 忽略 NTFS 命名规则,这或许可以解决问题。
git config --global core.protectNTFS false
关闭 protectNTFS 将阻止 Git 对基本名称为保留名称的文件发出警告,但如果文件名是保留名称之一,则不会阻止错误。
最佳解决方案是:
- 找出错误消息中提到的出错文件,例如('src/Assets/fonts/TTF/THICCCBOI-Black.ttf:Zone.Identifier'),就像我的情况一样。
- 将文件重命名为不与任何保留名称冲突的名称。例如,我可以将其重命名为“THICCCBOI-Black-Font.ttf”。注意:请在此处查看 Windows 文件命名文档。
- 如有必要,请在代码或配置中更新对已重命名文件的引用。
- 将更改提交到代码仓库。
参考链接
文章来源:https://dev.to/jumbo02/how-to-fix-error-invalid-path-when-working-with-git-on-windows-16kf