Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git Merge Conflicts Although the File Does Not Exist

Tags:

git

merge

Could you please help me to understand this behavior:

On branch develop I have a file with two commits:

BOCZA2@PC45819 MINGW64 /c/nttcloud/gitlab/coba/ZAA (develop)
$ git log --oneline -- coba.zaa.model/coba.zaa.model.config/src/main/java/com/zaa/model/config/Channel.java                      
8509cdf Added route generator based on freemarker template
1e890b7 Added XML configuration for test objects in StaticDataInitializer

Then I can switch to my feature branch:

BOCZA2@PC45819 MINGW64 /c/nttcloud/gitlab/coba/ZAA (develop)
$ git checkout feature/ZAA_tools_xmlgenerator
Switched to branch 'feature/ZAA_tools_xmlgenerator'
Your branch is up-to-date with 'origin/feature/ZAA_tools_xmlgenerator'.

And check that I have not commits for that file:

BOCZA2@PC45819 MINGW64 /c/nttcloud/gitlab/coba/ZAA (feature/ZAA_tools_xmlgenerator)
$ git log --oneline -- coba.zaa.model/coba.zaa.model.config/src/main/java/com/zaa/model/config/Channel.java

That file does not exist in working directory either:

BOCZA2@PC45819 MINGW64 /c/nttcloud/gitlab/coba/ZAA (feature/ZAA_tools_xmlgenerator)
$ cat  coba.zaa.model/coba.zaa.model.config/src/main/java/com/zaa/model/config/Channel.java
cat: coba.zaa.model/coba.zaa.model.config/src/main/java/com/zaa/model/config/Channel.java: No such file or directory

But when I try to merge develop into my feature branch:

BOCZA2@PC45819 MINGW64 /c/nttcloud/gitlab/coba/ZAA (feature/ZAA_tools_xmlgenerator)
$ git merge develop
[...]
Auto-merging         coba.zaa.model/coba.zaa.model.config/src/main/java/com/zaa/model/config/Channel.java
CONFLICT (content): Merge conflict in coba.zaa.model/coba.zaa.model.config/src/main/java/com/zaa/model/config/Channel.java
[...]
Automatic merge failed; fix conflicts and then commit the result.

And I get merge conflicts (both modified).

BOCZA2@PC45819 MINGW64 /c/nttcloud/gitlab/coba/ZAA (feature/ZAA_tools_xmlgenerator|MERGING)
$ git status -s coba.zaa.model/coba.zaa.model.config/src/main/java/com/zaa/model/config/Channel.java
UU coba.zaa.model/coba.zaa.model.config/src/main/java/com/zaa/model/config/Channel.java

What is the problem? The file doesn't exist in the feature branch! What is the reason for the merge conflict? Is my repository corrupt? How can I find out the reason?

like image 952
Adam Bogdan Boczek Avatar asked Nov 27 '15 14:11

Adam Bogdan Boczek


1 Answers

In my case I had this same issue because of the fact that file-renames in git are a run-time heuristic. (This is what Michael Monteith's answer proposes, but without a solution.)

git, at merge time, decided that the non-existing file in my branch was actually a rename of a file that does exist on my branch. Any files with 50% similarity in a single commit are considered a rename during the merge, by default. Asking git to merge with a higher threshold does the trick:

git merge master -X rename-threshold=100%

As to why I hit this issue: it's because our team decided on a single-squash-commit strategy for pull-requests. That means that individual commits on master are huge, thus multiplying the chance for "renames" within those single, massive commits.

like image 179
Johann Avatar answered Oct 21 '22 23:10

Johann