Posted almost 9 years ago. Visible to the public. Repeats.

Git: How to rebase your feature branch from one branch to another

In a nutshell: Use git rebase source-commit --onto target-branch

  • 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 (topmost = latest):

  • commit 6 [my-feature-branch]
  • commit 5
  • commit 4 [master]
  • commit 3
  • commit 2 [production]
  • commit 1

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 not work, as it will move commits 3 through 6 to production, effectively merging master into production. 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 --onto production master

That tells git you are moving the commits since master to production, and you'd end up with this history in your newly rebased branch:

  • commit 6 [my-feature-branch]
  • commit 5
  • commit 2 [production]
  • commit 1

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).

Once an application no longer requires constant development, it needs periodic maintenance for stable and secure operation. makandra offers monthly maintenance contracts that let you focus on your business while we make sure the lights stay on.

Owner of this card:

Arne Hartherz
Last edit:
almost 3 years ago
by Dominik Schöler
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Arne Hartherz to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more