I'm performing a git merge where a number of files have been moved, in the same way, in both branches of the merge. To my surprise, for about 10 files git has failed to find the version of the file in the branch I am merging from.
i.e. file starts off in /path/file.txt. In branch 1 file is modified, then moved to /path/newpath/file.txt. In branch 2 file is moved to the same path /path/newpath/file.txt. When I merge I would expect git to be able to deal with this. However, it shows this as a merge conflict, saying the files have been deleted from branch 1. I have three questions:
- Why does git fail to find these files? Naively I would expect git to simply say "get the version of this file path in the merge branch", but it presumably isn't doing this? It looks to me like the files involved have altered quite a lot, which leads me to suspect that git is comparing the file contents to establish if they are merge candidates, as suggested by this SO question : git merge with renamed files But surely files with the same path and name should be considered as merge candidates, even if their contents have changed quite a bit?
- Is there some config I can change to tell git to consider files with the same name and path as merge candidates? The SO question I referenced suggested that you can set the file similarity percentage, but this seems an oblique way to achieve what I want.
I can make this merge work by either manual or scripted update of my git index, to tell git the object hash of the file in the other branch. As per the script on the other SO question, it would look something like this:
FILE_PATH="path/to/file.txt"
git update-index --index-info <<EOI 000000 0000000000000000000000000000000000000000 0 $FILE_PATH 100644 e87d02f423c3a66da62ddc10b359314b34a556e3 2 $FILE_PATH 100644 0ddb2a448cb9cca97834df78ae00e213ecd9dd71 3 $FILE_PATH EOI
If I do so, do I need to also tell git about the common ancestor object? i.e. use update-index to put a entry for stage 1, in addition to the records for stages 2 and 3? The reason I'm confused is because in this specific case, the common ancestor will have a different path to both of the other versions, so even if I was to update the index to include it, how would that tell git that that was the ancestor!? What would tie that stage 1 entry to the stage 2 and 3 entries, given that the file path would be different?