Skip to content

Instantly share code, notes, and snippets.

@gbirke
Created June 16, 2025 14:42
Show Gist options
  • Save gbirke/d4e58a554e983a6fc00e7251732a866a to your computer and use it in GitHub Desktop.
Save gbirke/d4e58a554e983a6fc00e7251732a866a to your computer and use it in GitHub Desktop.
Rebase dependent branches in Git

Rebase dependent branches in Git

Problem: You rebased a branch (featureA) and now need to rebase a branch that depends on it (featureB).

Solution: Use git rebase --onto, which takes three parameters:

  1. The branch name of featureA
  2. The commit ID of the last commit in featureA, in the "old" log/history of featureB
  3. (optional) The branch name of featureB

There are to ways to get the "old" commit ID of the last commit in featureA: the commit history of featureB or the reflog.

Using the commit history of featureB

First, check out the branch for featureB and look at the log for featureB. You can look for one of two things:

  • How many commits are in featureB until you see the commits of featureA?
  • What is the commit ID of the last commit in featureA?

Relative commit ID

When counting the number of commits in featureB, you can use the tilde notation to reference "earlier" commits.

Example 1: Using the number of commits in featureB to reference to the last commit in featureA, featureB has 5 commits in this example:

git rebase --onto featureA featureB~5

If you have more than one commit in featureB, you should use the tilde notation and not the caret notation, (feature^5), because the caret notation is for addressing commit IDs of merged branches. See the following articles explaining the differences in the notation for Git's rev-parse

Absolute commit ID

Example 2: You have the commit ID of the last commit in featureA:

git rebase --onto featureA old-commit-id-of-featureA

Example 3: You have checked out a different branch entirely and still want to do the rebase from that branch (3rd parameter is no longer optional):

git rebase --onto featureA old-commit-id-of-featureA featureB

Using The Reflog

You can use this, where you just have rebased/changed featureA. In this case, the Git reflog has recorded (at least) two commit IDs for featureA:

  • featureA@{0} (or just featureA) is the most recent commit ID of featureA
  • featureA@{1} is the version of featureA before the rebase.

Example 4: Using the reflog:

git rebase --onto featureA featureA@{1} featureB

Sources:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment