gitcherry-pick

how to keep diverged form of a merged commit in a cherry-pick?


I want to cherry-pick a range of commit and among them, there is a merged commit which when I want to cherry-pick my commits, this merged commit just get cherry-picked in a linear form but I want to keep its diverged form and to see which commits are the parents of this merged commit but this situation isn't possible in a linear history created by cherry-pick, so I was wondering if it is possible to keep our history diverged in a cherry-pick or if isn't possible, is there any other alternative in git?

command I used:

git cherry-pick (commit C hash)~1..(commit F hash)   (my head is pointing to test1)

Diagrams:

A--B--C--E--F--K (master)               Current history
    \  \   /
     J   D (test2)
    (test1)
A--B--C--E--F--K--T (master)             History after cherry-picking
    \  \   /
     J   D (test2)
      \
       C
        \
         D
          \
           E
            \
             F
             (test1)
A--B--C--E--F--K--T (master)             Expected history after cherry-picking
    \  \   /
     J   D (test2)
      \
       C
      / \
     D   E
      \   \ 
       \---F   
          (test1)

Solution

  • One solution is interactive reabse (git rebase -i).

     git rebase -i -r --onto J C~ F
     # The edit screen pops up. Just save and quit.
    

    The expected history is created in a detached HEAD. Then update test1 with this new HEAD.

    git update-ref refs/heads/test1 HEAD
    

    The commits C, D, E and F are rebased onto J. -r/--rebase-merges instructs to preserve the merge commit structure.