Rebase Basics

Introduction

Rebase replays your branch commits on top of another branch tip—often producing a linear history without merge commits. It complements merge but has strict rules on shared branches. This chapter runs git rebase main, compares rebase vs merge, and introduces interactive squash as an advanced option.

Prerequisites

Visual: Merge vs Rebase

Before—main and feature diverged:

text
main:    A --- B --- C
              \
feature:       D --- E

After merge on main:

text
main:    A --- B --- C ------- M
              \             /
feature:       D --- E ------

After rebase feature onto main (then fast-forward merge):

text
main:    A --- B --- C --- D' --- E'
feature: (same tip as E')

Code explanation:

  • D' and E' are new commit IDs—same changes, new parents

Rebase Feature Branch Onto main

Update local main:

bash
git switch main
git pull origin main

Rebase feature:

bash
git switch feature/login
git rebase main

If clean, feature history sits directly after C.

Merge into main (often fast-forward):

bash
git switch main
git merge feature/login

Handle Conflicts During Rebase

Git pauses at each conflicting commit:

bash
# fix files, remove conflict markers
git add conflicted.js
git rebase --continue

Skip current commit (rare):

bash
git rebase --skip

Abort entire rebase:

bash
git rebase --abort

Code explanation:

  • Similar to merge conflicts—resolve, stage, continue

Golden Rule

Warning

Never Rebase Public History

Do not rebase commits that others already pulled—rewritten SHAs break their clones. Rebase your local feature branch before PR; do not rebase main after push without team agreement.

Safe pattern:

  1. You alone work on feature/x
  2. git rebase main locally
  3. git push --force-with-lease to update PR branch (only your feature)

Never force-push shared main.

rebase vs merge Summary

mergerebase
History shapeBranches visibleStraighter line
Commit IDsPreservedRewritten on feature
Shared branch safetyHighLow if already public
Learning curveLowerMedium

Teams pick one style—or merge to main, rebase locally only.

Interactive Rebase (Squash)

Combine last 3 commits on feature branch:

bash
git rebase -i HEAD~3

Editor lists commits with commands:

text
pick a1b2c3d Add login form
squash e4f5g6h Fix typo
squash f6g7h8i Fix typo again

Save—Git merges messages into one commit.

Code explanation:

  • pick keeps commit as-is
  • squash folds into previous commit
  • Use before PR for clean review—only on unpushed or personal branch

Pull With Rebase

Integrate remote main without merge commit on your branch:

bash
git pull --rebase origin main

Or set default:

bash
git config --global pull.rebase true

FAQ

Rebase deleted my commits?

They are orphaned until garbage collected—git reflog finds old tips.

PR says "needs rebase"?

Platform wants branch updated on latest main—rebase or merge main into feature locally, push.

rebase vs merge conflict count?

Rebase may conflict per replayed commit; merge often one conflict at merge point.

Can I rebase onto remote without fetch?

Run git fetch first—rebase needs accurate origin/main.

GitHub "Rebase and merge" button?

Platform rebases your commits onto base—similar effect, done server-side.

Interactive rebase scared me?

Use git rebase --abort—returns to pre-rebase state.