Home > Blockchain >  How to cherry-pick a merge commit the commits that were merged?
How to cherry-pick a merge commit the commits that were merged?

Time:07-09

If I have a branch that looks like:

M --- Merge Branch 'foobar' (sha ABC)
    o did stuff (sha DEF)
    o did other stuff (sha GHI)

and I cherry-pick the merge commit:

git cherry-pick -m1 ABC

Then I will have ABC, DEF, GHI all in one commit... I would like to actually just have the same structure from the other branch, and have the merge the individual commits separate.. How can I do that?

CodePudding user response:

Here are several ways to reach similar goals :

  • using rebase :
# start with a temp branch at commit DEF :
git checkout -b temp DEF

git rebase targetbranch
# or, if the fork point between 'foobar' and 'targetbranch' is not
# at GHI, instruct rebase to only take commits starting from GHI :
git rebase --onto targetbranch GHI^

git checkout targetbranch
git merge --no-ff temp
git branch -d temp
  • using cherry-pick :
# create a temp branch from your current tip :
git checkout -b temp

# list all commits to pick:
git cherry-pick GHI DEF

# merge temp branch into targetbranch:
git checkout targetbranch
git merge --no-ff temp
git branch -d temp
  • using cherry-pick (take 2):
# create a temp branch from your current tip :
git checkout -b temp

# the range of commits to pick should be :
# the range "first parent of M" .. "second parent of M"
git cherry-pick M^..M^2

# merge temp branch into targetbranch:
git checkout targetbranch
git merge --no-ff temp
git branch -d temp
  •  Tags:  
  • git
  • Related