-1

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?

patrick
  • 9,290
  • 13
  • 61
  • 112
  • 3
    Cherry pick the actual commits and then merge – Mad Physicist Jul 08 '22 at 00:56
  • I don't understand the diagram. Which way is time supposed to run here? What's the earliest of the three commits? And in what sense is it true that "Then I will have ABC, DEF, GHI all in one commit"? That seems a false premise. – matt Jul 08 '22 at 02:30
  • @matt I made a branch, I did GHI, and then DEF, then switched to master and did `git merge my-branch --no-ff` ... So now I have another branch I am on, and I want to pull all of that off of master, so it looks exactly as it does on master. – patrick Jul 08 '22 at 02:34
  • So why not cherry pick GHI, and then cherry pick DEF, and then cherry pick the merge commit? Actually what I would do is make a temporary branch and rebase, but it's exactly the same really, because rebase _is_ cherry pick. – matt Jul 08 '22 at 02:36
  • But cherry pick copies; perhaps "pull all of that off of master" means, not copy, but you wish those commits had never been on master to start with? Then just make a branch and reset master hard. That would be Regret Type 3: https://stackoverflow.com/questions/3528245/whats-the-difference-between-git-reset-mixed-soft-and-hard – matt Jul 08 '22 at 02:39
  • @matt because I have a million commits and am looking for a way to do this easily. I tried cherry-picking with a range `git cherry-pick DEF^..GHI` (though this example is bad because I actually have like >20 commits, not 2) but it was having conflicts which I could not understand why, so I aborted.. – patrick Jul 08 '22 at 22:00
  • Well that's why I said I would rebase. But conflicts are normal and you need to learn to deal with them. – matt Jul 08 '22 at 22:46

1 Answers1

0

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
LeGEC
  • 46,477
  • 5
  • 57
  • 104