HOWTO · Git

How to Rebase to a Specific Commit in Git

You can do a git rebase onto a specific commit in Git.

This article discusses the process of rebasing to a specific commit in Git. We will use several scenarios to illustrate how this can be accomplished.

We will employ the git rebase --onto command to rebase a branch to a specific commit. Let’s jump right in.

Rebase to a Specific Commit in Git

We use the --onto flag when we need to rebase a branch to another branch and specify the commits.

Basic Syntax:

git rebase –onto <new-parent> <old-parent> <head-of-new-parent>

Let’s look at example scenarios.

Here is the basic workflow we will be using:

Git Rebase to Previous Commit - Basic Workflow

Rebase new-feature to a Specific Commit in main

In our first scenario, we assume we wanted to use a commit in main without rebasing the entire branch. This will require us to note the SHA-1 of the commits in play.

We can use git log <branch-name> to get the hashes for the commits.

Git Rebase to Specific Commit - Workflow

In our case, we want to move the commits in the new-feature branch to the main branch at the 846e2fa commit (second last). We will run the following:

$ git rebase –onto 846e2fa bd9172c

The command above will have the effect shown below:

Git Rebase to Specific Commit - Output

Since we wanted to move the entire new-feature, we do not need to have a third argument in our command. Git moves the whole branch by default if you leave out the third argument.

Rebase new-feature to a Specific Commit in main and Remove the First Commit in new-feature

Let’s assume that our e2ff2bc is a dependency, and we want to move our branch to main at commit 846e2fa, which is the same dependency we have. In plain English, we want to rebase new-feature to main at 846e2fa and omit the first commit in new-feature.

Git Rebase to Specific Commit and Remove the First Commit - Workflow

Here is how we do it:

$ git rebase –onto 846e2fa e2ff2bc

The command above would have the effect shown below:

Git Rebase to Specific Commit and Remove the First Commit - Output

The git rebase -onto command takes the parent commit as a reference. In simpler terms, we are supposed to reference the commit before the commit we want to save.

Rebase new-feature to a Specific Commit in main and Remove the Last Commit in new-feature

Let’s say we want to move new-feature to 846e2fa but omit the commit d7dbeb in new-feature. How would we go about this?

Git Rebase to Specific Commit and Remove the Last Commit - Workflow

This is where the third argument comes in. We will run the following:

$ git rebase –onto 846e2fa bd9172c 730f163

This will result to:

Git Rebase to Specific Commit and Remove the Last Commit - Output

The third argument only points to the HEAD of the new parent commit; in our case, we have 730f163. This should delete the commit and all commits after it.

In a nutshell, Git allows us to rebase a branch to a specific commit. The git rebase -onto command accepts three arguments when rebasing.

Always remember that the third argument dictates the new parent commit.