Class 18 of 50 • Branching & Merging

Git Rebase: Creating Linear History & The Golden Rule of Rebasing

In large software teams, frequent merging creates a tangled "railroad track" commit graph full of meaningless merge commits like "Merge branch 'main' of github.com". Git Rebase solves this by taking your feature commits and replaying them cleanly on top of another base branch, producing a 100% linear history.

Git Rebase vs Merge Diagram
Figure 18.1: Git Merge (Preserving True Context with Merge Commit) vs Git Rebase (Replaying Commits into a Linear Stream).

How Git Rebase Works Conceptually

Imagine you branched feature/login off main at commit C2. While you worked on your feature, your colleagues merged commits C3 and C4 into main. Your branch is now behind.

If you run git rebase main while on feature/login:

  1. Git temporarily sets aside your feature commits.
  2. Git fast-forwards your branch to the tip of main (commit C4).
  3. Git reapplies (replays) each of your feature commits one-by-one on top of C4.

Because each commit is replayed on a new parent, it gets a brand-new SHA-1 commit hash!

# Step 1: Ensure you are on your feature branch:
$ git switch feature/login

# Step 2: Rebase on top of the latest main branch:
$ git rebase main
Successfully rebased and updated refs/heads/feature/login.

Rebase vs Merge: The Architectural Comparison

Property Git Merge Git Rebase
History Structure Non-linear; preserves true chronological order of when work occurred. Strictly linear; reads like a clean, continuous book from start to finish.
Merge Commits Creates explicit merge commits with multiple parents. Zero merge commits created.
Commit Hashes Existing commit hashes are never modified or re-created. Generates brand-new commit hashes for all replayed commits.
Traceability Easier to see when a feature was merged as a collective whole. Easier to read with git log and debug with git bisect.

The Golden Rule of Rebasing

The Golden Rule: Never Rebase Public Branches!

NEVER rebase commits that exist outside your local repository and that other people have based work on!

If you rebase commits already pushed to origin/main, you rewrite their commit hashes. When teammates pull, their Git will see divergent histories and create endless duplicate commits and merge nightmares. Use rebase only on your own private, local feature branches before opening a PR!

Explore Related Git & GitHub Tutorials & Next Steps

Strengthen your version control mastery by studying these connected classes:

Interactive Rebase (git rebase -i)

Squash, reword, drop, and edit commits in your private feature branch.

Git Merging Strategies

Contrast rebased linear histories against true chronological merge commits.

Recovering from Rebase Mistakes

Undo accidental rebases instantly using git reset --hard on your pre-rebase reflog.

Git Workflows: Trunk & Flow

Adopt modern rebasing standards in fast-paced trunk-based engineering teams.

Frequently Asked Questions (FAQ) - Student & Interview Review

Git will pause at the conflicting commit. Resolve the conflict in your editor, stage the resolved file (`git add [file]`), and then run `git rebase --continue` (do NOT run `git commit`!). If you want to abort, run `git rebase --abort`.
A linear history makes it trivial to trace regressions, roll back isolated features, perform bisecting bug hunts, and generate clean automated release changelogs without wading through hundreds of 'merge' commits.
Yes! Use `git reflog` to find the commit hash of your branch prior to rebasing, and run `git reset --hard [hash]` to instantly restore it.

Next Step in Your Git & GitHub Learning Path:

Advance your software engineering skills with the next topic in our synchronized curriculum.

Continue to Interactive Rebase (-i) → Previous: ← Merge Conflicts | Full Syllabus

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!