JesusValera

Git Rebase onto

How to modify the commit history with git rebase onto

 Found a typo? Edit me

When you are working with Git, you may find yourself in a situation where you need to rebase your branch onto another branch.
This can happen when you want to incorporate changes from one branch into another, but you want to keep the commit history clean and linear.

What is git rebase --onto?

The git rebase --onto is a powerful command when you need more control over the rebase process. It allows you to specify a different branch or commit to rebase your changes onto, which is not necessarily the upstream branch of the current branch.

The command takes three arguments:

  1. The new base commit
  2. The exclusive starting commit of the range to rebase
  3. The inclusive ending commit of the range to rebase

Syntax

git rebase --onto <new-base> <upstream> <branch>

When to use --onto?

1. Rebasing a Subset of Commits

When you want to rebase a subset of commits from your branch onto another branch:

     A---B---C  (feature)
    /
---X---Y---Z  (master)

If you want to rebase A into Z, you can use the following command:

(feature)$ git rebase --onto master Z

-- also works --
(feature)$ git rebase --onto master master feature

This command will rebase the commits from feature branch onto the master branch, starting from commit Z.

             A'---B'---C'  (feature)
            /
---X---Y---Z  (master)

2. Skipping Commits

When you want to skip a set of commits from your current branch:

             A---B---C---D  (feature)
            /
---X---Y---Z  (master)

If you want to skip A and B and rebase the rest onto Z, you can use the following command:

(feature)$ git rebase --onto master B feature

This skips commits A and B and applies the remaining commits onto the master branch.

             C'---D'  (feature)
            /
---X---Y---Z  (master)

3. Rebasing to a different base

When you want to rebase a branch onto a completely different base that is not its current base.

     A---B---C  (feature1)
    /
---X---Y---Z  (master)
    \
     D---E (feature2)

If you want to rebase feature2 onto feature1:

(feature1)$ git rebase --onto feature1 master feature2

This command will rebase feature2 onto feature1.

     A---B---C---D'---E'  (feature1)
    /
---X---Y---Z  (master)

References