Three scenarios that “git rebase –onto” can solve


3 Common & Frustrating Scenarios

When using Git, your commit history can sometimes become unintentionally complex. Here are some classic scenarios that many developers have faced.

Scenario 1: Accidentally Branched from Another Feature Branch

You were supposed to create your new work branch (my-feature) from the latest main, but you accidentally branched off a colleague’s work-in-progress branch (dev-feature). Now your Pull Request includes commits that have nothing to do with your work.

Scenario 2: Mixed Multiple Features in a Single Branch

While working on a branch (work-branch), you realize that commits C and D are actually for a completely different feature (new-idea). You want to cleanly separate just these two commits into a new branch.

Scenario 3: Started New Work on an Outdated Branch

You started some new work (new-work) on a branch you created months ago (old-feature). Meanwhile, the main branch has evolved significantly, and your new commits are now sitting on top of a very old history.

All of these problems can be solved with git rebase --onto.

The Key to the Solution: What is git rebase --onto?

git rebase --onto is a powerful rebase command that lets you specify exactly which commits to move (from..to) and where to move them (onto).

Basic Command Syntax

Understanding these three arguments is the key to mastering this command.

Practical Guide: Commands for Each Scenario

Scenario 1: Branched from Another Feature Branch

Let’s look at a concrete example for the most common case: “Scenario 1: Accidentally branched from another feature branch.”

Goal: Re-base the my-feature branch so it originates from main instead of dev-feature.

With this single command, Git reconstructs the history as follows:

The commits from my-feature (B’ and C’) have been cleanly moved on top of main, and are now independent of dev-feature.

Scenario 2: Separating Commits from a Branch

Goal: Move commits C and D from work-branch to a new new-idea branch, based on main. In this case, we use commit IDs directly.

This will place only commits C and D (as new commits) onto the main branch in your new new-idea branch.

Scenario 3: Moving from an Old Base to the Latest Base

Goal: Move the new-work branch from its outdated base (old-feature) to the tip of the latest main.

The command structure is identical to Scenario 1. rebase --onto works perfectly, no matter how far apart the branches are.

Important Safety Tips & Tricks

Pushing to a Remote Repository

Rebasing rewrites your local commit history. If you rebase a branch that has already been pushed to a remote, you must avoid git push --force, which can cause problems for your teammates. Instead, use the safer --force-with-lease.

Handling Conflicts During a Rebase

Since a rebase applies commits one by one, you may encounter a merge conflict. Don’t panic; follow these steps to resolve it.

  1. Resolve the conflicting files: Open the files in your editor and fix the code, removing the <<<<<<<, =======, and >>>>>>> markers.
  2. Stage the resolved files:
  3. Continue the rebase:

If you get overwhelmed and want to stop the rebase, you can always return to the state before you started with this command:

Conclusion

git rebase --onto might seem complex at first, but once you understand the concept of “move this range of commits onto that new base,” it becomes a powerful tool for tidying up your Git history.

If any of the scenarios described here feel familiar, give this command a try. Just remember to communicate with your team when rewriting the history of a shared branch.