git merge: apply changes to code that moved to a different file

后端 未结 4 874
忘了有多久
忘了有多久 2020-12-04 05:49

I am attempting a pretty beefy git merge maneuver right now. One problem that I am coming across is that I made some changes to some code in my branch, but my colleague mov

4条回答
  •  执念已碎
    2020-12-04 06:10

    Here is a merge solution of encountering a merge conflict with rename and edit and resolving it with mergetool recognizing the correct 3 merge source files.

    • After a merge fails because of 'deleted file' that you realize was renamed and edited:

      1. You abort the merge.
      2. Commit renamed files on your branch.
      3. And merge again.

    Walk-through:

    Create a file.txt:

    $ git init
    Initialized empty Git repository in /tmp/git-rename-and-modify-test/.git/
    
    $ echo "A file." > file.txt
    $ git add file.txt
    $ git commit -am "file.txt added."
    [master (root-commit) 401b10d] file.txt added.
     1 file changed, 1 insertion(+)
     create mode 100644 file.txt
    

    Create a branch where you will edit later:

    $ git branch branch-with-edits
    Branch branch-with-edits set up to track local branch master.
    

    Create the rename and edit on master:

    $ git mv file.txt renamed-and-edited.txt
    $ echo "edits on master" >> renamed-and-edited.txt 
    $ git commit -am "file.txt + edits -> renamed-and-edited.txt."
    [master def790f] file.txt + edits -> renamed-and-edited.txt.
     2 files changed, 2 insertions(+), 1 deletion(-)
     delete mode 100644 file.txt
     create mode 100644 renamed-and-edited.txt
    

    Swap to branch, and edit there too:

    $ git checkout branch-with-edits 
    Switched to branch 'branch-with-edits'
    Your branch is behind 'master' by 1 commit, and can be fast-forwarded.
      (use "git pull" to update your local branch)
    $ 
    $ echo "edits on branch" >> file.txt 
    $ git commit -am "file.txt edited on branch."
    [branch-with-edits 2c4760e] file.txt edited on branch.
     1 file changed, 1 insertion(+)
    

    Attempt to merge master:

    $ git merge master
    CONFLICT (modify/delete): file.txt deleted in master and modified in HEAD. Version HEAD of file.txt left in tree.
    Automatic merge failed; fix conflicts and then commit the result.
    

    Notice the conflict is hard to resolve - and that files were renamed. Abort, mimic the rename:

    $ git merge --abort
    $ git mv file.txt renamed-and-edited.txt
    $ git commit -am "Preparing for merge; Human noticed renames files were edited."
    [branch-with-edits ca506da] Preparing for merge; Human noticed renames files were edited.
     1 file changed, 0 insertions(+), 0 deletions(-)
     rename file.txt => renamed-and-edited.txt (100%)
    

    Try merge again:

    $ git merge master
    Auto-merging renamed-and-edited.txt
    CONFLICT (add/add): Merge conflict in renamed-and-edited.txt
    Recorded preimage for 'renamed-and-edited.txt'
    Automatic merge failed; fix conflicts and then commit the result.
    

    Great! Merge results in a 'normal' conflict that can be resolved with mergetool:

    $ git mergetool
    Merging:
    renamed-and-edited.txt
    
    Normal merge conflict for 'renamed-and-edited.txt':
      {local}: created file
      {remote}: created file
    $ git commit 
    Recorded resolution for 'renamed-and-edited.txt'.
    [branch-with-edits 2264483] Merge branch 'master' into branch-with-edits
    

提交回复
热议问题