Skip to content

Rebase

Rebase rewrites a series of commits onto a new base: it replays each commit of a branch onto a different starting point, producing new commits with new hashes, for a linear history.

Definition

What it is

git rebase main in a feature branch does three things. First, it finds the common ancestor of the feature branch and main. Second, it stores the feature branch's commits from that ancestor. Third, it moves the feature branch onto main's tip and replays the stored commits one by one. Each replayed commit gets a new hash, a new parent, and possibly a resolved conflict.

The result is a linear history: as if the developer had written every commit after the latest main. Merge commits disappear. The history looks like main → f1 → f2 → f3, not main ↘ f1 → f2 → f3 ↗ with a merge bubble.

Why it matters

A linear history is easier to read (no zig-zags), easier to bisect (no branching), and easier to review (git log --oneline shows the actual sequence of work). For projects with hundreds of commits a day, the noise of merge commits from two-line branches is significant.

The cost is real: rebase rewrites history, so it breaks every clone that has pulled the branch before the rebase. That is why the rule is "rebase only local, never shared branches".

Rebase vs merge

Aspect

Merge

Rebase

History shape

Bubble (two parents)

Linear

Hashes of commits

Preserved

Rewritten

Safe on shared branches

Yes

No

Conflicts

Once at merge

Per replayed commit

Audit trail

Full

Simplified

Typical use

main ← feature

feature → main

The rebase replay

graph LR
  C1[c1] --> C2[c2]
  C2 --> C3[c3 main]
  C1 --> F1[f1]
  F1 --> F2[f2]
  F2 --> F3[f3]
  C3 -.rebase.-> R1[f1]
  C3 -.rebase.-> R2[f2]
  R2 --> R3[f3]
  style C3 fill:#E8F5E9
  style R3 fill:#FFF3E0

When to rebase

Rebasing without breaking the team

  1. Local branches only

    The rule is simple: never rebase a branch others have pulled. If you already pushed, merge or force-with-lease.

  2. Interactive rebase before PR

    git rebase -i main lets you squash fixups, reorder commits, and rewrite messages. Do it before pushing.

  3. Use `--force-with-lease`

    If you must force push after rebase, use --force-with-lease instead of --force; it fails if someone else pushed.

  4. Rebase before merging feature to main

    Keeps the main history linear: your commits land on top of main, not as a bubble.

  5. Never rebase main

    Rewriting main breaks everyone. Merge into main, never rebase main.

Common failures

Origin

The term is literal: to re-base a series of commits, change its base. Git introduced rebase in 2005 as part of its initial design. Older systems (CVS, Subversion) had no equivalent: they could not rewrite history because their history was a linear chain. Git's DAG of commits made rebase possible. The term spread to other DVCS: Mercurial added hg rebase in 2008 as an extension, then as a built-in.

Rebase re-applies the commits on the current branch on top of the specified branch. The result is a new series of commits, each with a new hash.

— Chacon & Straub, *Pro Git*, 2014 (paraphrase)
2005 · Git introduce rebase en su diseño inicial
2008 · Mercurial añade rebase (extensión)
2010 · `--force-with-lease` disponible en Git 1.8.5 (2013)
2019 · `git rebase --rebase-merges` reescribe merges
1 · base nueva por rebase

Evidence

FAQ

When is rebase safe?

Only on branches nobody else has pulled. Rule of thumb: rebase only local, unpushed commits.

How to force push after rebase?

git push --force-with-lease, never plain --force. It aborts if the remote has moved.

Rebase vs merge for PRs?

Rebase before opening a PR (linear history for review). Merge with --no-ff for the PR itself if you want an audit trail.

Can I rebase main?

No. Rewriting main breaks every clone. Merge into main.

Connections

Etymology

Origin: compound

  • re (prefix): Latin, "again"
  • base (noun): Greek basis, "foundation, pedestal"

Semantic network

RebaseGitversion-coBranchMergeCommitStashforce-with

● broader ● narrower ● related

Broader

  • Git
  • version-control

Related

Sources