在Git中提交仅大小写不同的文件名更改

在Git中提交仅大小写不同的文件名更改

技术背景

在使用Git进行版本控制时,有时需要提交仅文件名大小写不同的更改。然而,由于某些文件系统(如Windows和OS X)默认是大小写不敏感的,这可能会导致Git无法正确识别这些更改。因此,需要一些特殊的方法来处理这种情况。

实现步骤

方法一:使用git mv命令

如果只是重命名文件而不是文件夹,可以使用git mv命令:

1
git mv -f relative/path/to/yOuRfIlEnAmE relative/path/to/yourfilename

在Git 2.0.1及以后的版本中,-f标志是多余的,但在旧版本中是必需的。

方法二:设置core.ignorecase

可以通过设置core.ignorecasefalse来告诉Git对文件名大小写敏感:

1
git config core.ignorecase false

但在大小写不敏感的文件系统上设置此选项通常不是一个好主意,可能会导致奇怪的错误。

方法三:修复整个仓库的文件名大小写

1
2
3
4
5
git rm -r --cached .
git add --all .
git status # 检查是否只有重命名的更改被暂存
git commit -a -m "Fixing file name casing"
git push origin main

方法四:使用SourceTree的UI操作

  1. FILE.ext重命名为whatever.ext
  2. 添加/暂存该文件
  3. whatever.ext重命名为file.ext
  4. 再次添加/暂存该文件

方法五:临时更改Git的大小写敏感性

单次命令更改

1
git -c core.ignorecase=true checkout mybranch

多次命令更改

  1. git config core.ignorecase 查看当前设置
  2. git config core.ignorecase <true or false> 设置新的设置
  3. 运行多个其他命令
  4. git config core.ignorecase <false or true> 恢复之前的设置

方法六:在GitHub上重命名文件

如果仓库托管在GitHub上,可以在GitHub上重命名文件并强制进行自上而下的文件重命名。具体步骤如下:

  1. 访问GitHub.com
  2. 导航到仓库并选择工作分支
  3. 找到要重命名的文件
  4. 根据文件是否可编辑进行相应操作:
    • 可编辑:点击“Edit this file”图标,更改文件名
    • 不可编辑:下载文件,重命名后上传
  5. 确保选择“Commit directly to the branchname branch”并提交更改
  6. 本地拉取分支

方法七:全局配置Git大小写敏感性

1
git config --global  core.ignorecase false

方法八:使用Python脚本批量处理

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

import os
import shlex
import subprocess

def run_command(absolute_path, command_name):
print( "Running", command_name, absolute_path )

command = shlex.split( command_name )
command_line_interface = subprocess.Popen(
command, stdout=subprocess.PIPE, cwd=absolute_path )

output = command_line_interface.communicate()[0]
print( output )

if command_line_interface.returncode != 0:
raise RuntimeError( "A process exited with the error '%s'..." % (
command_line_interface.returncode ) )

def main():
FILENAMES_MAPPING = \
[
(r"F:\\SublimeText\\Data", r"README.MD", r"README.md"),
(r"F:\\SublimeText\\Data\\Packages\\Alignment", r"readme.md", r"README.md"),
(r"F:\\SublimeText\\Data\\Packages\\AmxxEditor", r"README.MD", r"README.md"),
]

for absolute_path, oldname, newname in FILENAMES_MAPPING:
run_command( absolute_path, "git mv '%s' '%s1'" % ( oldname, newname ) )
run_command( absolute_path, "git add '%s1'" % ( newname ) )
run_command( absolute_path,
"git commit -m 'Normalized the \'%s\' with case-sensitive name'" % (
newname ) )

run_command( absolute_path, "git mv '%s1' '%s'" % ( newname, newname ) )
run_command( absolute_path, "git add '%s'" % ( newname ) )
run_command( absolute_path, "git commit --amend --no-edit" )

if __name__ == "__main__":
main()

核心代码

使用git mv重命名文件

1
git mv -f .\abcDEF.js  .\abcdef.js

设置core.ignorecase

1
git config core.ignorecase false

修复整个仓库的文件名大小写

1
2
3
4
git rm -r --cached .
git add --all .
git commit -a -m "Fixing file name casing"
git push origin main

最佳实践

  • 在进行文件名大小写更改之前,先备份重要文件,以防意外数据丢失。
  • 对于仅需更改少数文件大小写的情况,使用git mv命令或SourceTree的UI操作可能更方便。
  • 对于大规模的文件名大小写更改,使用Python脚本可以提高效率。
  • 在设置core.ignorecasefalse之前,确保了解可能带来的风险。

常见问题

移除了.gitignore中忽略但强制添加的文件

可以使用以下命令恢复:

1
git checkout HEAD -- ignored/folder/force-added.file

移除了文件的显式Git文件权限

可以手动重新应用权限,或重置文件到仓库中的状态:

1
git checkout HEAD -- path/to/file/with/executable-bit.set

处理子模块时出现警告

1
2
git rm --cached submodule/path
git submodule add <url-of-submodule-remote> submodule/path

在Git中提交仅大小写不同的文件名更改
https://119291.xyz/posts/2025-05-16.git-commit-case-sensitive-filename-changes/
作者
ww
发布于
2025年5月16日
许可协议