在OSX上,git合并已删除的文件,重命名为大写

时间:2013-10-09 19:30:38

标签: macos git merge git-merge

我编写了这个脚本并在OSX 10.8 Mountain Lion上运行它,在默认的不区分大小写的HFS文件系统上。

#!/bin/sh -x
# create git repo
rm -rf caps
git init caps
cd caps
# commit empty file called "file"
touch file
git add .
git commit -am "initial commit"
# create branch called "branch"
git branch branch
# rename "file" to "File"
# using --force per http://stackoverflow.com/questions/6899582
git mv --force file File
git commit -am "renamed capital"
# switch to branch, make a non-conflicting commit
git checkout branch
touch newfile
git add .
git commit -am "branch commit"
# merge master into branch, commit merge
git merge --commit --no-edit master
# but where's the renamed File?
more File

当脚本完成时,它在最后一行失败,应该成功:

+ git merge --commit --no-edit master
Removing file
Merge made by the 'recursive' strategy.
 file => File | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename file => File (100%)
+ more File
File: No such file or directory

git status显示了这个:

$ git status
# On branch branch
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   deleted:    File
#
no changes added to commit (use "git add" and/or "git commit -a")

我们可以使用git checkout File获取该文件,但此时很容易意外提交删除。

我们最近一直在重命名一些文件,修正了他们的资本,这对我们的团队来说很重要。我们可以使用git设置或最佳实践来解决此问题吗? (现在,我们只是告诉每个人要格外小心。)

2 个答案:

答案 0 :(得分:1)

该问题(“删除带有重命名大写的文件”)正好 commit ae352c7f37ef2098e03ee86bc7fd75b210b17683 David Turner (dturner-tw)解决的问题:

merge-recursive.c:修复案例更改合并错误

  

在不区分大小写的文件系统上,合并时,如果传入的提交已重命名,只会更改其大小写,则会从工作树中错误地删除该文件。
  合并重命名时,将删除具有旧名称的文件 - 但由于文件系统认为旧名称与新名称相同,因此新文件实际上将被删除。

     

我们通过不删除在阶段0的索引中具有案例克隆的文件来避免这种情况。

这意味着,启动git 2.0.1 +(2014年6月25日),合并现在应该有效。

答案 1 :(得分:0)

只有在重命名检测失败时才会出现此问题。

http://thread.gmane.org/gmane.comp.version-control.git/235889

git禁止重命名git 1.8中的空文件检测。 https://github.com/git/git/commit/4f7cb99ada26be5d86402a6e060f3ee16a672f16

此脚本按照预期在git 1.7和git 1.8上运行:

#!/bin/sh -x
# create git repo
git --version
rm -rf caps
git init caps
cd caps
git config --get core.ignorecase
# commit empty file called "file"
echo file > file
git add .
git commit -am "initial commit"
# create branch called "branch"
git branch branch
# rename "file" to "File"
# using --force per http://stackoverflow.com/questions/6899582
git mv --force file File
git commit -am "renamed to File"
# switch to branch, make a non-conflicting commit
git checkout branch
echo newfile > newfile
git add .
git commit -am "branch commit"
# merge master into branch, commit merge
GIT_EDITOR=true git merge --verbose --commit master
ls File
git status

但是由于git中的错误,这个脚本失败了:

#!/bin/sh -x
# create git repo
git --version
rm -rf caps
git init caps
cd caps
git config --get core.ignorecase
# commit empty file called "file"
echo file > file
git add .
git commit -am "initial commit"
# create branch called "branch"
git branch branch
# rename "file" to "File"
# using --force per http://stackoverflow.com/questions/6899582
git mv --force file File
echo "completely different content" > File
git commit -am "renamed to File"
# switch to branch, make a non-conflicting commit
git checkout branch
echo newfile > newfile
git add .
git commit -am "branch commit"
# merge master into branch, commit merge
GIT_EDITOR=true git merge --verbose --commit master
ls File
git status
相关问题