In a nutshell: Use git rebase --onto target-branch source-commit
-
target-branch
means "branch you want to be based on" -
source-commit
means "commit before your first feature commit"
Let's say my-feature-branch
is based on master
and we want it to be based on production
. Consider this history:
Here, master
has commits that are not yet in production
(number 3 and 4).
Just doing a simple git rebase production
from my-feature-branch
will do nothing, as production
is already in its history. Merging the feature branch into production
would now merge commits 3 through 6, including commit 3 and 4 from master
.
This is not what we want.
Instead, we want to chop off our commits since master
(number 5 and 6) and place them on production
. We need to do this (while being in my-feature-branch
):
git rebase master --onto production
This tells git you are moving the commits since master
to production
, and you'd end up with this:
Note that after doing that, you will have changed your branch's history and need to do a forced push, if it was on origin before.
As always, you can supply commit hashes, not just branch names (which are only shortcuts to a commit anyway).