我通过将第一个字母去大写来更改了一些文件名,如name.jpg中的name.jpg。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()

其他回答

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

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

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

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

git mv -f yOuRfIlEnAmE yourfilename

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

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

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

有时临时更改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>>-将配置值设置回以前的设置。

修复整个repo上的git文件名大小写:

git rm -r --cached .
git add --all .

git status ##Review that **only** changes staged are renames

## Commit your changes after reviewing:
git commit -a -m "Fixing file name casing"
git push origin master

@Uriahs Victor评论解释:

此命令实际执行的操作是删除git认为仍然存在的文件/文件夹名称。所以它会清除缓存,但将所有内容保留在当前文件夹中您的本地更改),但它会发现其他错误情况文件夹/文件不再存在,因此将在git中显示为已删除地位然后你可以向上推github,它将删除错误案例的文件夹/文件。这个答案有一个图形描述命令的含义。