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

后端 未结 4 872
忘了有多久
忘了有多久 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 05:52

    I had a similar issue, and I resolved it by rebasing my work to match the target file organization.

    Say that you modified original.txt on your branch (the local branch), but on the master branch, original.txt has been copied to another one, say copy.txt. This copy has been done in a commit that we name commit CP.

    You want to apply all your local changes, commits A and B below, that were made on original.txt, to the new file copy.txt.

     ---- X -----CP------ (master)
           \ 
            `--A---B--- (local)
     
    

    Create a throwaway branch move at the starting point of your changes with git branch move X. That is to say, put the move branch at commit X, the one before the commits you want to merge; most likely, this is the commit from which you branched out to implement your changes. As user @digory doo wrote below, you can do git merge-base master local to find X.

     ---- X (move)-----CP----- (master)
           \ 
            `--A---B--- (local)
     
    

    On this branch, issue the following renaming command:

    git mv original.txt copy.txt
    

    This renames the file. Note that copy.txt did not yet exist in your tree at this point.
    Commit your change (we name this commit MV).

            ,--MV (move)
           /
     ---- X -----CP----- (master)
           \ 
            `--A---B--- (local)
     
    

    You can now rebase your work on top of move:

    git rebase move local
    

    This should work without problem, and your changes are applied to copy.txt in your local branch.

            ,--MV (move)---A'---B'--- (local)
           /
     ---- X -----CP----- (master)
     
    

    Now, you do not necessarily want or need to have commit MV in your main branch's history, because the move operation may lead to a conflict with the copy operation at commit CP in the main branch.

    You only have to rebase your work again, discarding the move operation, as follows:

    git rebase move local --onto CP
    

    ... where CP is the commit where copy.txt was introduced in the other branch. This rebases all the changes on copy.txt on top of the CP commit. Now, your local branch is exactly as if you always modified copy.txt and not original.txt, and you can continue merging with others.

                    ,--A''---B''-- (local)
                   /
     -----X-------CP----- (master)
     
    

    It is important that the changes are applied on CP or otherwise copy.txt would not exist and the changes would be applied back on original.txt.

    Hope this is clear. This answer comes late, but this may be useful to someone else.

提交回复
热议问题