1

I can't seem to figure this Git scenario out after reading the man page and searching some of the questions here, so I hope some of you can help me out.

Here's the situation. I've got two branches, let's call them master and experimental, with the commit graph looking like this:

A---B---C---D  master
     \
      E---F---G---H  experimental

and I want to end up here:

A---B---C---D---F---H  master
     \
      E---F---G---H  experimental

I could achieve this by successively cherry-picking all relevant commits from experimental, but I'd like to use git rebase -i to get a list of all commits and select the relevant ones. Is this possible?

Mark
  • 1,306
  • 13
  • 19
  • It seems like the cherry pick would be easier as it doesn't require any temp branches. Keep in mind that you can specify ranges in the cherry pick command. In your case, `git cherry-pick ..experimental` would cherry pick G and H – mwarsco Jul 18 '14 at 14:54
  • Yeah, for ranges cherry-pick would be okay, but it's quite inconvenient to list all commits manually, especially if there are quite a few and they are not successive. And I like being able to `git rebase --abort` if things go awry. – Mark Jul 18 '14 at 14:57

1 Answers1

3

It is possible, but first, mark your experimental branch with a new 'tmp' branch:

git checkout experimental
git checkout -b tmp

Then do your rebase:

git rebase -i master

And drop all the commits you don't want.

         (master)
            |
A---B---C---D---F'---H'  tmp
     \
      E---F---G---H  experimental

Finally, merge master to tmp

git checkout -B master # reset master to tmp
git branch -d tmp

Since a rebase moves a branch, and since it is master that has to change, jthill points out in the comments the shortest solution:

git checkout -B master experimental
git rebase -i master@{1}

See more on HEAD@{1} at:

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Yup. I'd just `git checkout -B master` rather than the heckout/merge, because we're resetting the branch label, why bother with the two-step and worktree churning. – jthill Jul 18 '14 at 14:35
  • Thanks! I'd tried lots of combinations, and almost got there, but didn't think to create a temporary branch. This works excellently. – Mark Jul 18 '14 at 14:52
  • @Mark yes, since a rebase moves the branch, and since you want to keep 'experimental' in place... a tmp branch was mandatory. – VonC Jul 18 '14 at 14:53
  • 1
    For maximum concision, `git checkout -b master experimental; git rebase -i master@{1}`... – jthill Jul 18 '14 at 16:16
  • @jthill good point, and I have added it to the answer, but let's not be *too* concise here: it is useful to understand what takes place behind the scene, in order to get why your shorter version works. – VonC Jul 18 '14 at 16:32