AlgoMaster Logo

Rebase --onto

Last Updated: January 3, 2026

5 min read

In the world of Git, rebase --onto often feels like a hidden gem—powerful and flexible, yet underappreciated. It allows developers to move commits from one branch to another while reparenting them to a new base.

This capability can save you from complicated merges or help you rearrange your commit history in a cleaner, more logical way.

Understanding how to leverage rebase --onto effectively can elevate your Git skills significantly, allowing you to manage and maintain your project's history with precision.

Understanding rebase --onto

To grasp the power of rebase --onto, it’s essential first to understand its syntax and how it differs from a standard git rebase. The basic command looks like this:

Here's what each part represents:

  • <newbase>: This is the commit that you want your commits to be based on after the rebase.
  • <upstream>: This indicates the starting point for the commits you want to move, effectively telling Git which commits to consider.
  • <branch>: This is the branch that contains the commits you wish to rebase.

What this command does is take the commits from <branch> that are after <upstream> and rebase them onto <newbase>.

By understanding the purpose of each argument, you can begin to visualize how rebase --onto can be employed in various scenarios.

Practical Examples

Let’s put rebase --onto into practice with some concrete examples. Suppose you have the following commit history:

In this scenario, you have three commits (A, B, C) on the master branch and three commits (D, E, F) on the feature branch. Now, imagine you want to move commits D, E, and F to be based on commit C instead of B.

To achieve this, you would use:

After executing this command, your commit graph would look like this:

Notice how the original D, E, and F have been rewritten as D', E', and F'. This is because git rebase creates new commits with the same content but new parent commits.

Use Cases for rebase --onto

The rebase --onto command shines in various scenarios where traditional rebasing methods may fall short. Here are some common use cases:

1. Restructuring Feature Branches

Imagine you’ve been working on a feature branch that has diverged significantly from the main branch. You realize that some commits should be based on a more recent version of master.

Using rebase --onto, you can quickly reparent those commits to the latest point.

2. Isolating Commits for Cherry-Picking

Suppose you have multiple commits in a feature branch, but only a few should be included in the next release. Instead of cherry-picking each commit one by one, you can rebase only the relevant commits onto a release branch.

Example:

If you want to move commits D and E to another branch:

This command effectively takes the last two commits from feature and moves them onto release, leaving out the unwanted commit F.

3. Editing a Series of Commits

Sometimes, you might have a series of commits that need to be edited before merging into the main branch. With rebase --onto, you can move the commits to a temporary branch, edit them, and then merge them back.

Navigating the Complexity of rebase --onto

While rebase --onto is powerful, it does introduce a level of complexity that can confuse even seasoned developers. It's crucial to visualize the changes you are making, especially if you are dealing with multiple branches or complex histories.

Keeping Track of Your Commits

One common pitfall is losing track of your commit references. Before performing a rebase --onto, consider using git log or git reflog to inspect your commit history. This will help you confirm the commit IDs you need for the command.

This command provides a clear visual representation of your commit history.

Handling Conflicts

Just like with any rebase, conflicts can occur when using rebase --onto. Be prepared to resolve these conflicts as they appear. After resolving the conflicts, you can continue the rebase with:

This command will apply the remaining commits after the conflict resolution.

Common Mistakes to Avoid

To navigate the complexities of rebase --onto successfully, it’s vital to be aware of frequent mistakes that developers make:

1. Misidentifying Commit References

Always double-check that you’re referencing the correct commits. A common mistake is to accidentally specify the wrong <upstream> or <newbase>, leading to unexpected results.

2. Forgetting to Create Backups

Before performing any rebase operations, especially with --onto, it’s good practice to create a backup branch. You can do this simply by running:

This way, if anything goes wrong during the rebase, you can easily revert to your backup.

3. Ignoring the Impact on Others

If you’re working in a shared repository, be cautious with rebasing. Rebasing rewrites history, which can lead to confusion for other developers. Always communicate with your team before performing significant rebases.

Conclusion

The rebase --onto command is a powerful tool in Git that allows you to manipulate your commit history with precision. Understanding its syntax and use cases can streamline your workflows and help you maintain a clean project history.

With the ability to restructure branches, isolate commits, and edit series of commits, rebase --onto opens up a world of possibilities for managing your Git repositories.

Now that you understand how to effectively use rebase --onto, you are ready to explore the nuances of squashing commits.

In the next chapter, we will examine how to consolidate your commit history into meaningful, logical units, enhancing both readability and collaboration in your projects.