解决 Git 报错 fatal: unable to checkout working tree: File exists 的排查指南
排查与解决 Git 克隆或切换分支提示 unable to checkout working tree: File exists 文件大小写冲突的完整方案。
问题现象
在 Windows 或 macOS 操作系统中克隆仓库或切换分支时,操作失败中断:
error: unable to create file 'src/Utils.ts': File exists
fatal: unable to checkout working tree
warning: Clone succeeded, but checkout failed.
工作区处于不完整检出状态,提示文件已存在。
触发根因
- 操作系统文件系统大小写敏感性差异(核心诱因):Linux 文件系统默认大小写敏感(区分
utils.ts与Utils.ts为两个独立文件)。而在 Windows(NTFS)和 macOS(APFS 默认)上,文件系统不区分大小写。 - 远程仓库在 Linux 环境下被提交了两个仅大小写不同的同名文件(例如
config.json和Config.json)。在 macOS/Windows 检出第一个文件后,尝试创建第二个同名文件时,操作系统判定文件已存在而报错。
解决步骤与操作命令
步骤一:开启 Git 本地忽略大小写配置
在本地仓库配置 core.ignorecase:
git config core.ignorecase true
步骤二:在 Linux 环境下重命名冲突文件(彻底根除)
在 Linux 服务器或 GitHub 网页端将冲突文件之一进行彻底重命名:
# 在 Linux 环境下执行:
git mv src/Utils.ts src/StringUtils.ts
git commit -m "fix: rename Utils.ts to avoid case conflict on macOS/Windows"
git push origin main
步骤三:利用 sparse-checkout 临时排除冲突文件检出
若暂时无法修改仓库历史,通过稀疏检出排除冲突文件:
git config core.sparseCheckout true
echo "/*" > .git/info/sparse-checkout
echo "!src/Utils.ts" >> .git/info/sparse-checkout
git read-tree -mu HEAD
验证确认
再次运行代码检出:
git checkout -f main
工作区所有文件顺利解压检出,未再出现 File exists 拦截。


