September 26, 2020

git rebase --onto for feature branches

I need to perform this series of actions every few weeks, but can never remember the commands.

  1. branch off of dev to make feature-1
  2. put feature-1 up for code review, and start working on feature-2 by branching from feature-1
  3. feature-1 is eventually merged into dev, so feature-2’s commits need to change to be on top of dev as well
git rebase --onto dev feature-1 feature-2

assuming you already have branch feature-2 checked out (because you are actively working on it), you can remove the last argument (the target branch to change)

git rebase --onto dev feature-1

so it reads like:

git rebase --onto <to> <from> <branch-to-manipulate>

more details at git-scm.com

I like this quote:

The current branch is reset to <upstream>, or <newbase> if the —onto option was supplied. This has the exact same effect as git reset —hard <upstream> (or <newbase>). ORIG_HEAD is set to point at the tip of the branch before the reset.