Exploring the ‘git rebase –onto’ command (with examples)

Updated: January 27, 2024 By: Guest Contributor Post a comment

Introduction

Version control systems are essential for modern software development, providing a way to track changes, collaborate with others, and maintain a history of a project’s evolution. Git, one of the most popular version control systems, offers a plethora of commands to manage your repositories effectively. Among these commands is git rebase --onto, an advanced feature that provides fine-grained control over rewriting commit history.

This tutorial aims to demystify git rebase --onto and demonstrate its use through practical examples. By the end, you’ll have a solid understanding of how to use this powerful command in different scenarios to maintain a clean and linear project history.

Basic Understanding of Rebase

Before diving into --onto, let’s understand the basic concept of rebase. Rebasing is the process of moving or combining a sequence of commits to a new base commit. Essentially, it’s a way to apply your work on top of another branch. The default behavior of git rebase is to take the commits from the current branch and replay them on the tip of the targeted branch.

git checkout feature-branch
git rebase master

The above commands will start the rebase process to move the commits from feature-branch on top of master.

What Does –onto Do?

The --onto flag allows you to specify a new base that is different from the default branch onto which you are rebasing. It is particularly useful when you want to move a series of commits onto a different branch or to a specific commit within a branch.

General Syntax

git rebase --onto new-base up-to upstream

Where:

  • new-base is the commit onto which you want to move your commits.
  • up-to is the commit just before the first one you want to move.
  • upstream is the branch you want to change the base of.

Example 1: Basic git rebase –onto Usage

Let’s start with a simple scenario where you have a feature-branch that is branched off from develop, and you want to rebase it onto master.

# Start on feature-branch
$ git checkout feature-branch

# Rebase the commits onto master
$ git rebase --onto master develop

In this example, we’re changing the base of feature-branch from develop to master. The --onto master specifies the new base, and develop is where the feature-branch originally branched off.

Example 2: Moving Specific Commits

Sometimes, you may need to move a group of specific commits onto a different branch. Let’s say you want to move commits C3 and C4 from feature-branch onto master, but not C1 and C2.

# Assuming C2 is the commit just before C3
$ git rebase --onto master C2 feature-branch

In the above example, we use the SHA-1 hash or the reference to C2 as the up-to argument, and feature-branch as the upstream.

Example 3: Isolating a Feature

Imagine you have a feature branch, and within it, there’s a specific feature you’ve developed that you’d like to move onto a new isolated-feature branch.

$ git branch isolated-feature C4 # Create a branch at commit C4
$ git rebase --onto isolated-feature C3 feature-branch

C4 is the commit you’ve created the new branch off, isolated-feature, and C3 is just before the commit range you want to isolate. Your commits will now be replayed onto isolated-feature instead of feature-branch.

Handling Conflicts

Rebase operations may result in conflicts. As you replay commits, Git tries to apply changes onto the new base, but sometimes these changes can clash. If you encounter a conflict, Git will stop at the offending commit and let you resolve the issue. Once resolved, you can continue the rebase process with:

$ git rebase --continue

Should you decide to abort the rebase and return to the original state, use:

$ git rebase --abort

Advanced Scenario: Interactive Rebase

Interactive rebase, activated with the -i option, gives you even more control. Let’s say you want to re-order, edit, or squash commits as you move them:

# Perform an interactive rebase onto master
$ git rebase -i --onto master develop feature-branch

After running this command, an editor will open, allowing you to manipulate the commits as desired before completing the rebase.

Conclusion

In this guide, we explored the git rebase --onto command through multiple examples, from basic usage to handling specific commits and advanced scenarios. Remember, while rebase is a powerful tool to maintain a clean history, it rewrites it, so use it judiciously, especially on shared branches.