我通过将第一个字母去大写来更改了一些文件名,如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

其他回答

若并没有工作,使用git-rm文件名从磁盘中删除文件并将其添加回。

我制作了一个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()

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

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

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

我们可以使用gitmv命令。例如,如果我们将文件abcDEF.js重命名为abcDEF.js,那么我们可以从终端运行以下命令

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