Search code examples
gitgit-mergegit-mv

merging branches after moving files to a different directory


I have two branches that I try to merge. In the original branch I have some files that in the 2nd branch where moved to a different location. the problem is that when I try to merge from the original branch, it doesn't apply the changes to the new file location even tough it detected that I moved the files with git move.

I created the following scenario to better explain my problem:

I have branch a with files a/1.txt, a/2.txt. in branch b I did git mv to 1.txt and 2.txt from directory a to b.

next.. in branch a I modified a/1.txt, in branch b when I try merge branch a into it, it tries to create a/1.txt instead of applying the change on the new file's location (in directory b).

how do I resolve this issue ?


Solution

  • I provided a small example that most probably shows your problem:

    mkdir test
    cd test/
    git init
    echo -e '1\n2\n3' >foo
    git add foo
    git commit -m foo
    git checkout -b bar
    echo -e '1\n4\n5' >bar
    rm foo
    git add --all .
    git commit -m bar
    git checkout -
    echo -e '6\n2\n3' >foo
    git add foo
    git commit -m foo2
    git merge bar
    

    Now you get an error saying

    # CONFLICT (modify/delete): foo deleted in bar and modified in HEAD. Version HEAD of foo left in tree.
    # Automatic merge failed; fix conflicts and then commit the result.
    

    If you instead decrease the level of similarity that is necessary to identify a rename with -X find-renames=30% (similarity was 33%, as one of three lines was the same), the merge works better:

    git merge --abort
    git merge bar -X find-renames=30%
    git mergetool
    git commit --no-edit