2

Is there a way to move a sequential set of commits from one (local) branch to another? I have searched quite a bit and still haven't found a clear answer for what I want to do.

For example, say I have this:

master A---B---C
                \
feature-1        M---N---O---P---R---Q

And I have decided that the last 3 commits would be better off like this:

master  A---B---C
                 \
feature-1         M---N---O
                           \
f1-crazy-idea               P---R---Q

I know I can do this, and it does work:

$ git log --graph --pretty=oneline (copying down sha-1 ID's of P, R, Q)
$ git checkout feature-1
$ git reset --hard HEAD^^^
$ git checkout -b f1-crazy-idea
$ git cherry-pick <P sha1>
$ git cherry-pick <R sha1>
$ git cherry-pick <Q sha1>

I was hoping that instead there would be a more concise way to do this, possibly with git rebase, although I haven't had much luck.

Any insight would be greatly appreciated.

Thanks,

Jamie

jpswain
  • 14,642
  • 8
  • 58
  • 63
  • The general answer (not needed in this simple case) would need a `rebase --onto`: http://stackoverflow.com/questions/1994463/how-to-cherry-pick-a-range-of-commits-and-merge-into-another-branch/1994491#1994491 or http://stackoverflow.com/questions/2369426/how-to-move-certain-commits-to-another-branch-in-git/2369516#2369516 or http://stackoverflow.com/questions/2100851/how-to-undo-a-commit-and-commit-the-changes-into-the-other-branch-in-git/2100953#2100953 – VonC Apr 12 '10 at 04:08

1 Answers1

6

There is (git rebase --onto), but in this case you don't need to move them. Checkout feature-1, create a new branch f1-crazy-idea at the same spot (but stay on feature-1), and hard reset backwards 3

Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175