█
LastWrite
  • > Curriculum
  • > Pricing
  • > For Educators
  • > About
  • > Contact
Log InGet Started

Questions, concerns, bug reports, or suggestions? We read every message, write to us at [email protected].

More ways to reach us →
LastWrite

Structured computer science lessons for aspiring developers and security professionals.

[email protected]

(201) 785-7951

Mon–Fri, 9 AM–5 PM EST

Learn

  • Curriculum
  • Pricing

Company

  • About
  • For Educators & Schools
  • Contact Us

Legal

  • Terms of Service
  • Privacy Policy
© 2026 LastWrite. All rights reserved.
Curriculum/Software Engineering/Git and GitHub Pro/Rebasing vs Merging
45 minIntermediate

Rebasing vs Merging

After this lesson, you will be able to: Decide between rebase and merge for any given situation, run interactive rebase to clean up commit history, and avoid the rewrite-shared-history pitfall.

Rebase and merge both combine work from two branches. The choice between them determines what your history looks like and how easy it is to debug six months from now.

Prerequisites:Branching Strategies

Merge vs rebase, mechanically

Merge: creates a new commit with two parents. Preserves both histories; result tree is the combination. History looks like a graph. Rebase: rewrites your commits as if they had been made on top of the new base. History stays linear; each commit looks 'cleaner'. Same final code state in most cases; very different history shape.

When to use which (a practical rule)

Rebase your feature branch onto main BEFORE opening a PR. Linear history is easier to review. Merge the PR into main when it lands (most teams use 'squash merge' which collapses the PR's commits into one on main, the cleanest default). NEVER rebase a branch that other people have already pulled. You rewrite SHAs; their local copies become orphaned. Pull main with `git pull --rebase` so your local main stays linear with origin.

Interactive rebase: clean up your branch before review

Squash 'WIP' commits, fix typos in messages, reorder commits. The PR your reviewer sees should tell a story.

tsx
# On your feature branch, before opening the PR:
git rebase -i origin/main
# Opens an editor with one line per commit:
# pick a1b2c3d Add user table migration
# pick d4e5f6a WIP
# pick 7g8h9i0 fix typo in migration
# pick j1k2l3m WIP debug
# pick n4o5p6q add user creation endpoint
#
# Change 'pick' to:
# pick -> keep this commit as-is
# squash -> combine into the previous commit's message
# fixup -> combine into previous, DROP the message
# reword -> keep the commit but rewrite the message
# drop -> remove the commit entirely
#
# Save & exit; resolve any conflicts if asked.
# Force-push (safely): git push --force-with-lease

💡 Always use --force-with-lease, never --force

`--force-with-lease` only force-pushes if the remote branch is what you last saw. If a teammate pushed something while you were rebasing, `--force-with-lease` refuses; `--force` would have silently overwritten their work. Make it a habit; train your fingers.

Squash merge vs merge commit vs rebase merge

Squash merge: the entire PR collapses into one commit on main. Clean history, lose intermediate detail. The most popular default. Merge commit: keeps every commit + a merge commit. Verbose history; useful for very long-running branches. Rebase merge: replays each commit linearly onto main. No merge commit. Most precise; can be confusing in cross-team reviews. GitHub lets repo admins enforce one style; pick one and stick with it.

Common mistakes only experienced Git users avoid

Rebasing main onto your feature branch (backwards). The rebase target is what you want to be on top of; main goes on the BOTTOM. Force-pushing to a shared branch (e.g. main, develop). Set up branch protection so this fails at the server. Resolving rebase conflicts in a hurry and losing changes. Each conflict file is a small decision; don't speed-resolve. Rebasing 100+ commits at once. Break into smaller rebases. Treating rebase as 'better' than merge dogmatically. They're tools for different situations.

Quick Check

You've pushed 8 commits to your PR branch and a colleague has commented on commit #3. You want to clean up the history before merge. What command preserves their context while still cleaning up?

Pick the safest path.

Sign in and purchase access to unlock this lesson.

Sign in to purchase
←Branching Strategies
Back to Git and GitHub Pro
Advanced Git: cherry-pick, reflog, bisect, submodules→