git: move commit before merge

Question

My history tree currently looks like this: enter image description here

I'd like to apply commit b3 to branch master. Of course I could merge again branch feature into master but history will look messy with two merge commits (a6, and a4 that is just useless now): enter image description here


Thus, what I'd like to know, is how to make a4 now point on b3 instead of b2? enter image description here I acknowledge SHA1s will be different, and thus commits will be renamed a4' and a5'


Show source
| git   | merge   2017-01-02 20:01 1 Answers

Answers ( 1 )

  1. 2017-01-02 21:01

    From the master branch, you can simply rebase onto the new b3 while preserving merges using the --preserve-merges option (or -p in short):

    git rebase -p feature
    

    That way, when Git rebases, it will not attempt to flatten the merge, but instead recreate it on top of the new base commit. So your history will look like this:

                                   master
                                     ↓
    a1 -- a2 -- a3 --------- a4' -- a5'
            \                /
             \              /
              b1 -- b2 -- b3
                          ↑
                       feature
    

    Compared to the following when not using the --preserve-merges flag:

                                          master
                                            ↓
    a1 -- a2                 a3' -- a4' -- a5'
            \                /
             \              /
              b1 -- b2 -- b3
                          ↑
                       feature
    
◀ Go back