Git Rebase Onto Another Branch

Learn how to use git rebase to replay your branch commits on top of another branch. Keep a clean, linear project history every time.

git rebase main

Detailed Explanation

What Does git rebase main Do?

The git rebase command takes the commits from your current branch and replays them on top of another branch — in this case, main. Unlike merging, which creates a merge commit, rebasing rewrites history to produce a linear sequence of commits.

How It Works

  1. Git identifies the common ancestor of your current branch and main.
  2. Git temporarily removes the commits unique to your branch.
  3. Git fast-forwards your branch pointer to the tip of main.
  4. Git re-applies each of your commits, one by one, on top of the updated base.

Step-by-Step Example

# Switch to your feature branch
git checkout feature/login

# Rebase onto the latest main
git rebase main

If there are conflicts, Git will pause and let you resolve them. After resolving, run git rebase --continue to proceed, or git rebase --abort to cancel the entire operation.

When to Use Rebase

  • Before opening a pull request — rebasing ensures your branch applies cleanly on top of the latest main, avoiding unnecessary merge commits.
  • To keep history linear — teams that prefer a clean git log use rebase over merge for integrating upstream changes.

Important Caveats

Never rebase commits that have already been pushed to a shared branch. Rebasing rewrites commit hashes, which forces collaborators to reconcile divergent histories. If you must update a pushed branch, use git push --force-with-lease instead of --force for added safety.

Rebasing is one of the most powerful tools in Git, but it demands discipline. When used correctly, it produces a project history that is easy to read, easy to bisect, and easy to revert.

Use Case

A developer wants to update their feature branch with the latest changes from main before opening a pull request, keeping the commit history linear.

Try It — Git Command Builder

Open full tool