faq,

Git 常见问题 - git 文件不区分大小写问题

git

现象

创建一个 git 仓库 git-test,并提交一个文件 test.go,然后修改文件名的大小写Test.go

1
2
3
4
5
6
7
8
9
10
11
12
13
$ mkdir git-test
$ cd git-test
$ git init --initial-branch=main
$ touch test.go
$ git add .
$ git commit -m "feat: add test.go"

$ mv test.go Test.go
$ ls
Test.go
$ git status
On branch main
nothing to commit, working tree clean

可以看到 git status 没有发生改变,也就是说本地修改的文件名没有在 git 生效

解决方法

  1. 移除本地缓存

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    $ git rm -r --cached test.go
    rm 'test.go'
    
    $ git status
    On branch main
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
    	deleted:    test.go
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    	Test.go
    
  2. 配置 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
    
    $ git config core.ignorecase false
    
    $ git add .
    $ git status
    On branch main
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
    	renamed:    test.go -> Test.go
    
    $ git commit -m "refactor: rename test.go"
    
    $ git log
    commit 8828b606ebc810bf8510487391cdeff4b86027ee (HEAD -> main)
    Author: CatchZeng <xxxxxxxx@xx.com>
    Date:   Fri Jun 17 15:59:53 2022 +0800
    
        refactor: rename test.go
    
    commit d8d4b8716c2000bb2dfec2b6bf873887de76d590
    Author: CatchZeng <xxxxxxxx@xx.com>
    Date:   Fri Jun 17 15:43:07 2022 +0800
    
        feat: add test.go
    (END)
    

参考


CatchZeng
Written by CatchZeng Follow
AI (Machine Learning) and DevOps enthusiast.