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


当前回答

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

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

那么你可以这样使用它:

git-lowercase-file Name.jpg

其他回答

我接受了@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()

在OSX下,为了避免这个问题并避免在不区分大小写的文件系统上开发时出现其他问题,可以使用Disk Utility创建区分大小写虚拟驱动器/磁盘映像。

运行磁盘实用程序,创建新的磁盘映像,并使用以下设置(或根据需要进行更改,但要区分大小写):

确保告诉git它现在在区分大小写的FS上:

git config core.ignorecase false

Git有一个配置设置,告诉它是否需要区分大小写的文件系统:core.ignorecase。要让Git保持大小写稳定,只需将此设置设置为false。(如果您已经推送了文件,请务必小心,然后在给出其他答案后,应首先移动它们)。

git config core.ignorecase false

注意,在不区分大小写的文件系统上,将此选项设置为false通常是一个坏主意。这样做会导致奇怪的错误。例如,以仅更改字母大小写的方式重命名文件将导致git报告虚假冲突或创建重复文件(来自Mark Amery的评论)。

文档

从git-config文档中:

核心.ignorecase如果为true,则此选项启用各种变通方法,使git能够更好地处理不区分大小写的文件系统,如FAT。例如,如果一个目录列表在git期望makefile时找到了makefile,git将假设它实际上是同一个文件,并继续将其记为makefile。默认值为false,但gitclone(1)或gitinit(1)将在创建存储库时探测并将core.ignorecase设置为true(如果合适)。

不区分大小写的文件系统

据我所知,最流行的两种具有不区分大小写文件系统的操作系统是

窗户操作系统X

Mac OSX High Sierra 10.13在一定程度上修复了这一问题。只需为git项目创建一个虚拟APFS分区,默认情况下它没有大小限制,也不占用空间。

在磁盘实用程序中,选择容器磁盘时单击+按钮在格式下选择APFS(区分大小写)命名为敏感利润可选:在Sensitive中创建一个名为git和ln-s/Volumes/Sensitive/git/Users/johndoe/git的文件夹

您的驱动器将位于/卷/敏感/

如何在Git中提交仅区分大小写的文件名更改?

使用SourceTree,我可以从UI完成所有这些

将FILE.ext重命名为whatever.ext暂存该文件现在将whatever.ext重命名为file.ext再次暂存该文件

这有点乏味,但如果你只需要对几个文件进行处理,那就很快了