How to Rebase Git Branch Onto Another Parent Branch

We had a long running feature that I originally branched off of for a new feature. When I finished, I realized I needed it before our long running feature would be done. Since it didn’t have any code changes that were dependent on our big feature, I thought it would be easy to just rebase it. Turns out, you need a special kind of rebase.

That’s when I started digging into the –onto flag. When you want to “replay” your feature branch’s commits on another branch—while keeping the feature branch—you can use this:

git rebase --onto

This is what the docs (git rebase --help) show to illustrate this scenario:


First let's assume your topic is based on branch next. For example, a feature developed in topic depends on some functionality which is found in next.
o---o---o---o---o  master
    \
        o---o---o---o---o  next
                        \
                        o---o---o  topic
We want to make topic forked from branch master; for example, because the functionality on which topic depends was merged into the more stable master branch. We want our
tree to look like this:
o---o---o---o---o  master
    |            \
    |             o'--o'--o'  topic
    \
        o---o---o---o---o  next
We can get this using the following command:
git rebase --onto master next topic