git merge conflict due to moved files

前端 未结 1 1276
春和景丽
春和景丽 2021-01-13 01:28

I have not done so much with git so far. Now, I have the following problem. While I did some local modifications and commits, my colleague restructured files and folders wit

1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-13 02:13

    If, as a result, you want to keep your changes, but in the new files location, using mergetool (and thus having to choose between deleting the files, and so your changes, or keeping the files, but they won't be in the good directory) will not be satisfactory in any case.

    In this situation, while in conflict state, I would move the conflicting files (which are in the old directory) to the new directory, git add the new files, git rm the old files (conflicting) and commit.

    Below and trace of the procedure (3 files "file1", "file2" and "file3" have been moved in a "files" folder and updated in parallel in another branch, the branch doing the move has beed merged first and then the branch updating the files content is merged after and conflicting):

    ghislain@debian: /tmp/git-test (master)
    > git merge --no-ff add-file-content 
    CONFLICT (modify/delete): file3 deleted in HEAD and modified in add-file-content. Version add-file-content of file3 left in tree.
    CONFLICT (modify/delete): file2 deleted in HEAD and modified in add-file-content. Version add-file-content of file2 left in tree.
    CONFLICT (modify/delete): file1 deleted in HEAD and modified in add-file-content. Version add-file-content of file1 left in tree.
    Automatic merge failed; fix conflicts and then commit the result.
    ghislain@debian: /tmp/git-test (master *+|MERGING)
    > git status
    On branch master
    You have unmerged paths.
      (fix conflicts and run "git commit")
    
    Unmerged paths:
      (use "git add/rm ..." as appropriate to mark resolution)
    
        deleted by us:   file1
        deleted by us:   file2
        deleted by us:   file3
    
    no changes added to commit (use "git add" and/or "git commit -a")
    ghislain@debian: /tmp/git-test (master *+|MERGING)
    > mv file1 file2 file3 files
    ghislain@debian: /tmp/git-test (master *+|MERGING)
    > git status
    On branch master
    You have unmerged paths.
      (fix conflicts and run "git commit")
    
    Unmerged paths:
      (use "git add/rm ..." as appropriate to mark resolution)
    
        deleted by us:   file1
        deleted by us:   file2
        deleted by us:   file3
    
    Changes not staged for commit:
      (use "git add ..." to update what will be committed)
      (use "git checkout -- ..." to discard changes in working directory)
    
        modified:   files/file1
        modified:   files/file2
        modified:   files/file3
    
    no changes added to commit (use "git add" and/or "git commit -a")
    ghislain@debian: /tmp/git-test (master *+|MERGING)
    > git add files
    ghislain@debian: /tmp/git-test (master *+|MERGING)
    > git status
    On branch master
    You have unmerged paths.
      (fix conflicts and run "git commit")
    
    Changes to be committed:
    
        modified:   files/file1
        modified:   files/file2
        modified:   files/file3
    
    Unmerged paths:
      (use "git add/rm ..." as appropriate to mark resolution)
    
        deleted by us:   file1
        deleted by us:   file2
        deleted by us:   file3
    
    ghislain@debian: /tmp/git-test (master *+|MERGING)
    > git rm file1 file2 file3
    file1: needs merge
    file2: needs merge
    file3: needs merge
    rm 'file1'
    rm 'file2'
    rm 'file3'
    ghislain@debian: /tmp/git-test (master +|MERGING)
    > git status
    On branch master
    All conflicts fixed but you are still merging.
      (use "git commit" to conclude merge)
    
    Changes to be committed:
    
        modified:   files/file1
        modified:   files/file2
        modified:   files/file3
    
    ghislain@debian: /tmp/git-test (master +|MERGING)
    > git commit
    [master 4e478c6] Merge branch 'add-file-content'
    ghislain@debian: /tmp/git-test (master)
    > 
    
    

    0 讨论(0)
提交回复
热议问题