我通过将第一个字母去大写来更改了一些文件名,如name.jpg中的name.jpg。Git无法识别这些更改,我不得不删除这些文件并再次上传。在检查文件名的更改时,Git是否可以区分大小写?我没有对文件本身进行任何更改。


当前回答

只要您只是重命名文件而不是文件夹,就可以使用gitmv:

git mv -f yOuRfIlEnAmE yourfilename

(在Git2.0.1中进行了修改,上面的咒语中的-f标志是多余的,但在较旧的Git版本中需要。)

其他回答

如果您正在进行更复杂的更改,如目录名大小写更改,您可以在Linux机器上进行更改,因为Linux本身(以及Linux上的git)将名称相同但大小写不同的文件/目录视为完全不同的文件或目录。

因此,如果您在Windows上,您可以使用WSL安装Ubuntu,在那里克隆您的repo,使用VSCode打开克隆的repo目录(使用WSL远程扩展从Windows访问WSL Ubuntu),然后您将能够通过VSCode进行重命名,并使用VSCode git集成提交/推送它们。

当你做了很多文件重命名,其中一些只是改变了大小写,很难记住是哪一个。手动“git移动”文件可能需要一些工作。因此,在文件名更改任务期间,我要做的是:

将所有非git文件和文件夹删除到不同的文件夹/存储库。提交当前的空git文件夹(这将显示为所有文件已删除。)将所有文件添加回原始git文件夹/存储库。提交当前非空的git文件夹。

这将解决所有的案例问题,而不需要找出您重命名的文件或文件夹。

我接受了@CBarr的回答,并编写了一个Python 3脚本,用一个文件列表来实现:

#!/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的大小写敏感度是有用的。

方法#1-更改单个命令的大小写敏感性:

git-c core.ignorecase=true checkout mybranch,关闭单个checkout命令的区分大小写功能。或者更一般地:git-c core.ignorecase=<<true或false>><<command>>。(感谢VonC在评论中提出了这一点。)

方法#2-更改多个命令的大小写敏感性:

要更改设置更长时间(例如,如果在更改回之前需要运行多个命令):

git-config-core.ignorecase(返回当前设置,例如false)。git-config-core.ignorecase<<true或false>>-设置所需的新设置。…运行多个其他命令。。。git-config-core.ignorecase<<false或true>>-将配置值设置回以前的设置。

我制作了一个bash脚本,将存储库文件名小写:

function git-lowercase-file {
  tmp="tmp-$RANDOM-$1"
  git mv -f $1 $tmp
  git mv -f $tmp ${1,,}
}

那么你可以这样使用它:

git-lowercase-file Name.jpg