Git rebase from one branch to another

radu c picture radu c · Jan 15, 2014 · Viewed 7.6k times · Source

I have the following case :

                          K---L new-feature
                         /
                H---I---J dev-team1
               /
              E---F---G dev-main
             /
A---B---C---D master

And I want to move only the new-feature (K---L) branch in dev-main branch without (H---I---J) form dev-team1

                H---I---J dev-team1
               /
              E---F---G---K---L dev-main
             /
A---B---C---D master

Answer

Ash Wilson picture Ash Wilson · Jan 15, 2014

git rebase has an --onto argument that does what you need.

git checkout new-feature
git rebase --onto dev-main dev-team1

# Now new-feature has commits K' and L' after G in dev-main.
git checkout dev-main
git merge --ff-only new-feature

See the man page for "git rebase" for more details. (I also like to add a -i just to double check that I'm moving the commits that I think I am.)

You could also use git cherry-pick, especially if the number of commits is small:

git checkout dev-main
git cherry-pick K L