Rebasing a Git merge commit

后端 未结 5 925
滥情空心
滥情空心 2020-11-27 09:10

Take the following case:

I have some work in a topic branch and now I\'m ready to merge back to master:

* eb3b733 3     [master] [origin/master]
| * b6         


        
5条回答
  •  执笔经年
    2020-11-27 10:06

    Given that I just lost a day trying to figure this out and actually found a solution with the help of a coworker, I thought I should chime in.

    We have a large code base and we have to deal with 2 branch heavily being modified at the same time. There is a main branch and a secondary branch if you which.

    While I merge the secondary branch into the main branch, work continues in the main branch and by the time i'm done, I can't push my changes because they are incompatible.

    I therefore need to "rebase" my "merge".

    This is how we finally did it :

    1) make note of the SHA. ex.: c4a924d458ea0629c0d694f1b9e9576a3ecf506b

    git log -1
    

    2) Create the proper history but this will break the merge.

    git rebase -s ours --preserve-merges origin/master
    

    3) make note of the SHA. ex.: 29dd8101d78

    git log -1
    

    4) Now reset to where you were before

    git reset c4a924d458ea0629c0d694f1b9e9576a3ecf506b --hard
    

    5) Now merge the current master into your working branch

    git merge origin/master
    git mergetool
    git commit -m"correct files
    

    6) Now that you have the right files, but the wrong history, get the right history on top of your change with :

    git reset 29dd8101d78 --soft
    

    7) And then --amend the results in your original merge commit

    git commit --amend
    

    Voila!

提交回复
热议问题