📌

Skip Some Commits When Doing Merge

Question

How to skip some commits when doing merge? This situation is not a common one but an interesting one. Look at the picture above if you don’t understand the question.

Solution 1 - cherry-pick

[master]$ git cherry-pick 123474
[master]$ git cherry-pick 123475

And then the tree is like this:

              474  475
    ---o---o---o---o---> dev
   |
   |              351 474  475 
---o---o---o---o---o---o---o---> master
   

Solution 2 - rebase

[bug]$ git branch temp 123475 # a
[bug]$ git rebase --onto master 123474^ temp # b
[bug]$ git checkout master # c
[master]$ git reset --hard temp # d

after a:

 o---o---o---o---o  master
         \
          o---o---o---o---o   bug
                           \
                            temp

after b:

                   o`---o` temp
                  /
 o---o---o---o---o  master
         \
          o---o---o---o---o   bug

after d:

 o---o---o---o---o---o`---o` HEAD -> master
         \
          o---o---o---o---o   bug

Solution 3 - rebase -i

[dev]$ git checkout -b bug-to-merge-into-master
[bug-to-merge-into-master]$ git rebase -i 123471 123475
d 123471
d 123472
d 123473
p 123474
p 123475
[bug-to-merge-into-master]$ git checkout master
[master]$ git merge bug-to-merge-into-master

Solution 4 - merge + revert

[master]$ git merge dev
[master]$ git revert -n 123471
[master]$ git revert -n 123472
[master]$ git revert -n 123473
[master]$ git commit -m "revert"

Now the tree is like this:

      471 472 473 474 475
    ---o---o---o---o---o
   |                    \
   |                      \
---o---o---o---o---o---o---o--------o----->master
                          merge   revert

Solution 5 - using patch

[bug]$ git format-patch 123474^..123475 --stdout > ~/Downloads/part.patch
[bug]$ git checkout master
[master]$ git am --signoff < ~/Downloads/part.patch

Reference

在我们一生中,命运赐予我们每个人三个导师,三个朋友,三名敌人,三个挚爱。但这十二人总是不以真面目示人,总要等到我们爱上他们、离开他们、或与他们对抗时,才能知道他们是其中哪种角色。