我试图重命名一个文件有不同的大小写,从它之前:

git mv src/collision/b2AABB.js src/collision/B2AABB.js
fatal: destination exists, source=src/collision/b2AABB.js, destination=src/collision/B2AABB.js

正如您所看到的,Git对此进行了处理。我也尝试过使用普通的旧mv命令重命名,但Git没有接收重命名(作为重命名或作为新的未跟踪文件)。

如何将文件更改为相同名称的不同大写?我在Mac OS X v10.7.3 (Lion)上,Git 1.7.9.1,使用Z shell (zsh) 4.3.15。


当前回答

Vonc的回答是完全正确的,但仍然有一个潜在的情况,你的重命名操作将无法在windows git中工作:

假设你想把dir/mypath重命名为dir/mypath:

git mv dir/mypath dir/myPath

但它没有报告:

Rename from 'dir/mypath' to 'dir/mypath' failed. Should I try again? (y/n) 

问题是bash已经无声地将命令行dir/myPath替换为dir/myPath,因为它已经检测到存在这样一个路径,但大小写不同。

解决方案是使用中间移动操作:

git mv dir/mypath dir/mypath_temp
git mv dir/mypath_temp dir/myPath

其他回答

我在Sourcetree中对Windows进行了以下更改:

我通过从文件系统中删除文件来解决这个问题:

然后简单地丢弃我想要保留的文件并提交:

现在一切都按预期进行。

基于这个答案:

https://stackoverflow.com/a/66121726/3850405

工作的例子:

git mv ./src/images/poster_video.PNG ./src/images/poster_video.png

这个Python代码片段将git mv——强制目录下的所有文件都是小写的。例如,foo/Bar.js将通过git mv foo/Bar.js foo/Bar.js——force变成foo/Bar.js。

根据你的喜好修改它。我只是想分享:)

import os
import re

searchDir = 'c:/someRepo'
exclude = ['.git', 'node_modules','bin']
os.chdir(searchDir)

for root, dirs, files in os.walk(searchDir):
    dirs[:] = [d for d in dirs if d not in exclude]
    for f in files:
        if re.match(r'[A-Z]', f):
            fullPath = os.path.join(root, f)
            fullPathLower = os.path.join(root, f[0].lower() + f[1:])
            command = 'git mv --force ' + fullPath + ' ' + fullPathLower
            print(command)
            os.system(command)

OS X下的文件名不区分大小写(默认情况下)。这与其说是Git的问题,不如说是操作系统的问题。如果删除并读取该文件,则应该得到您想要的内容,或者将其重命名为其他内容,然后再重命名回来。

考虑到larsks的回答,你可以用“——force”命令让它工作:

 git mv --force myfile MyFile