How to move certain commits to be based on another branch in git? How to move certain commits to be based on another branch in git? git git

How to move certain commits to be based on another branch in git?


This is a classic case of rebase --onto:

 # let's go to current master (X, where quickfix2 should begin) git checkout master # replay every commit *after* quickfix1 up to quickfix2 HEAD. git rebase --onto master quickfix1 quickfix2 

So you should go from

o-o-X (master HEAD)     \       q1a--q1b (quickfix1 HEAD)              \               q2a--q2b (quickfix2 HEAD)

to:

      q2a'--q2b' (new quickfix2 HEAD)     /o-o-X (master HEAD)     \       q1a--q1b (quickfix1 HEAD)

This is best done on a clean working tree.
See git config --global rebase.autostash true, especially after Git 2.10.


You can use git cherry-pick to just pick the commit that you want to copy over.

Probably the best way is to create the branch out of master, then in that branch use git cherry-pick on the 2 commits from quickfix2 that you want.


The simplest thing you can do is cherry picking a range. It does the same as the rebase --onto but is easier for the eyes :)

git cherry-pick quickfix1..quickfix2