描述
在 Windows 或者 macOS 系统中,对于已经提交到仓库中的文件修改文件名的大小写,然后又用 git rm 命令将老文件从 Git 仓库删除掉,并保存下新的文件,这个时候,再进行切换分支的操作,就会出现这个提示:
复现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# 新建测试文件夹并切换到这个文件夹下面 mkdir test-checkout-error && cd test-checkout-error # 新建空 Git 仓库 git init # 新建测试文件 user.php touch user.php # 暂存新增文件 git add . # 发现文件名大小写错误,进行修正 mv user.php User.php # 再次暂存新增文件 git add . # 提交到仓库 git commit -m "add user.php and User.php" # 在当前分支基础上新建分支 dev git checkout -b dev # 删除索引中的 user.php git rm --cached user.php # 提交到仓库 git commit -m "delete user.php" # 切换分支,报错 git checkout master error: The following untracked working tree files would be overwritten by checkout: user.php Please move or remove them before you can switch branches. Aborting |
分析
由于 Windows 或者 macOS 系统对于文件名是不区分大小写的,如果在 Git 仓库中应用了 ignorecase 的缺少配置 false,那么 Git 会区分文件名的大小写。
解决
在当前项目目录下执行
1 |
git config core.ignorecase true |
然后再切换分支,进行分支合并等操作之后,再执行
1 |
git config --unset core.ignorecase |
删除刚才的配置操作。
参考链接: