相关技巧

关注公众号 jb51net

关闭
首页 > 网络编程 > 相关技巧 > git一次大小写目录名导致远程仓库出现两套代码的踩坑

git一次大小写目录名导致远程仓库出现两套代码的踩坑及解决

作者:码农研究僧

你是否遇到过Windows下修改目录大小写后,本地git status正常但远程仓库出现重复目录?本文揭秘根本原因:NTFS大小写不敏感与Git的core.ignorecase配置,并提供实战步骤清理索引中的重复路径,避免团队协作冲突

前言

在 Windows 上开发,有时候我们会顺手把目录名从小写改成驼峰,比如 emailconfig → emailConfig

改完后一看本地文件正常,IDE 也没报错,就放心地继续开发了

push 到远程后,却发现——仓库里居然同时存在 emailconfig 和 emailConfig 两套代码! 更诡异的是,本地 git status 干干净净

打开远程仓库

ruoyi-ui/src/views/system/emailconfig/      ← 旧的,还在!
ruoyi-ui/src/views/system/emailConfig/      ← 新的,也在!

本地执行 git status,输出却极其干净:

On branch xx
Your branch is up to date with 'origin/xx'.

1. 原理

1.Windows 的 NTFS 默认是大小写不敏感的

emailconfig 和 emailConfig 在 Windows 眼里是同一个文件夹

手动重命名后,文件系统只是更新了显示名称,底层路径对 Git 来说"看起来没变"

2.Git 的 core.ignorecase 默认是 true

Git 在 Windows 安装时,会自动把 core.ignorecase 设为 true

这意味着 Git 默认忽略仅大小写变化的文件名/目录名变更

3.远程仓库(Linux)是大小写敏感的

2. 实战

先确认 Git 索引里到底存了几套路径:

git ls-files | findstr "emailconfig"

输出:

ruoyi-ui/src/views/system/emailconfig/components/ConfigModal.vue
ruoyi-ui/src/views/system/emailconfig/components/TestModal.vue
ruoyi-ui/src/views/system/emailconfig/index.vue
ruoyi-ui/src/views/system/emailConfig/components/ConfigModal.vue
ruoyi-ui/src/views/system/emailConfig/components/TestModal.vue
ruoyi-ui/src/views/system/emailConfig/index.vue

实锤了:Git 索引里同时存在 emailconfig 和 emailConfig 两套记录

而 git status 之所以不显示,是因为 Windows 文件系统把这两个路径映射到了同一个物理目录,Git 认为"工作区和索引一致"

截图如下:

具体步骤如下:

# 1. 关键:让 Git 临时区分大小写,防止误删大写的目录
git config core.ignorecase false

# 2. 只从 Git 索引中删除旧的小写目录(--cached 不碰本地文件)
git rm -r --cached ruoyi-ui/src/views/system/emailconfig

# 3. 把正确的大写目录重新加入索引
git add ruoyi-ui/src/views/system/emailConfig

# 4. 查看状态,应该能看到大量 delete 和 rename
git status

# 5. 提交
git commit -m "refactor: remove duplicate emailconfig, keep emailConfig"

# 6. 推送
git push origin xx

# 7. 恢复设置(防止以后踩坑)
git config core.ignorecase true

注意事项:

  1. 不要直接 rd /s /q 删除 Windows 大小写不敏感,直接删除 emailconfig 可能会误删 emailConfig 的物理文件
  2. 不要长期保持 ignorecase=false 这个配置在团队协作中容易引发冲突,改完记得恢复

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文