Git重命名检测空文件

Mat*_*dge 5 git merge file-rename

我最近遇到了一个问题,即一个空文件在两个分支中以不同方式重命名但合并时没有引发冲突.

重新创建的步骤如下.

  1. 创建一个空文件.

    git init
    touch empty
    git add empty
    git commit -m "add empty file"
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在分支中重命名它.

    git checkout -b branch
    git mv empty empty-in-branch
    git commit -m "empty -> empty-in-branch"
    
    Run Code Online (Sandbox Code Playgroud)
  3. 在主服务器中以不同方式重命名.

    git checkout master
    git mv empty empty-in-master
    git commit -m "empty -> empty-in-master"
    
    Run Code Online (Sandbox Code Playgroud)
  4. 合并分支到主人.

    git merge --no-commit branch
    
    Run Code Online (Sandbox Code Playgroud)

这给出了消息Automatic merge went well; stopped before committing as requested.

git status只显示新文件empty-in-branch.但是没有删除,empty-in-master所以如果我们在这个阶段提交,我们将获得这两个文件.

我希望这会被标记为需要手动解决的合并冲突(即决定保留哪个空文件).如果原始文件非空,那就会发生这种情况.

有关影响重命名检测的空文件有什么特别之处吗?我可以添加任何参数git merge来检测冲突(例如调整合并策略)吗?

oni*_*ake 2

自此提交以来,不再考虑在递归合并时重命名空文件:https://github.com/git/git/commit/4f7cb99ada26be5d86402a6e060f3ee16a672f16

旧版本的 Git 仍然将其报告为冲突。

$ git --version
git version 1.7.9.5
# ... follow OP instructions to reproduce
$ git merge --no-commit branch
CONFLICT (rename/rename): Rename "empty"->"empty-in-master" in branch "HEAD" rename "empty"->"empty-in-branch" in "branch"
Automatic merge failed; fix conflicts and then commit the result.
$ git status
# On branch master
# Unmerged paths:
#   (use "git add/rm <file>..." as appropriate to mark resolution)
#
#   both deleted:       empty
#   added by them:      empty-in-branch
#   added by us:        empty-in-master
#
no changes added to commit (use "git add" and/or "git commit -a")
Run Code Online (Sandbox Code Playgroud)

Git 不会隐式跟踪重命名,因此如果没有重命名检测,最终发生的情况是 Git 只会看到两次提交都删除了该文件empty,并且每次提交都添加了一个新文件。

有一个选项可以更改 git-diff 的此行为(https://github.com/git/git/commit/90d43b07687fdc51d1f2fc14948df538dc45584b),但目前它没有在git merge.

其他合并策略似乎也没有给出所需的行为。