git checkout 提示 “error: The following untracked working tree files would be overwritten by checkout”

转载自http://t.zoukankan.com/imzhi-p-solution-to-git-checkout-error-tips.html

问题描述
Windows 或者 macOS 操作系统中,文件名是不区分大小写的。对于已经提交到仓库中的文件修改文件名的大小写,然后又用 git rm 命令将老文件从 Git 仓库删除掉,并保存下新的文件,这个时候,再进行切换分支的操作,就会出现这个提示:
在这里插入图片描述

复现

新建测试文件夹并切换到这个文件夹下面

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

2、然后再切换分支,进行分支合并等操作之后,再执行

git config --unset core.ignorecase

删除刚才的配置操作。

参考链接:git - The following untracked working tree files would be overwritten by checkout - Stack Overflow

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值