Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git: move commit before merge

Tags:

git

merge

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'

like image 879
ebosi Avatar asked Jan 02 '17 19:01

ebosi


1 Answers

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
like image 163
poke Avatar answered Oct 11 '22 15:10

poke