How to avoid git rebase killing merge commits?

前端 未结 2 1820
孤独总比滥情好
孤独总比滥情好 2020-12-23 20:26

Given the following git history:

    C-I    origin/master
   /
A-B-F-G-H  master
 \\   /
  D-E      branch-b

I want to rebase

相关标签:
2条回答
  • Add --preserve-merges to your rebase command. In case there were conflict resolutions in your merge, add 'recursive theirs' strategy as a parameter as well.

    EDIT: --preserve-merges is now deprecated, use --rebase-merges instead

    0 讨论(0)
  • 2020-12-23 20:57

    This isn't going to be very pretty but I think you can do it.

    Rebase F onto the origin/master as your new master branch:

    git checkout F
    git checkout -b new_master
    git rebase origin/master
    

    Merge branch-b into your new branch:

    git merge branch-b
    

    Cherry pick the remaining H commit onto your new master branch:

    git cherry-pick master
    

    Delete your old master branch:

    git branch -D master
    

    Unfortunately you will also have to do the merge again (hopefully it doesn't take any manual merging).

    I didn't actually try this out, so I would make a backup of the repository first, but I am pretty confident that you will get what you want. I also suggest opening up gitk --all and refreshing the tree with "F5" after each command so you can see what is changing.

    Someone else should still post if they know of a more elegant way to do it.

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