Git Rebase vs. Merge: A Practical Guide
When should you rebase and when should you merge? A clear, example-driven breakdown of the trade-offs, plus a simple workflow you can adopt today.
merge and rebase solve the same problem — combining work from two branches — but they shape your history very differently. Here’s how to choose.
The one-sentence summary
Merge preserves history exactly as it happened. Rebase rewrites history to look like work happened in a straight line. Neither is “correct”; they’re tools for different goals.
How merge works
A merge takes the tips of two branches and ties them together with a new merge commit:
git checkout main
git merge feature
Your history keeps every commit and shows the branch points. This is honest and non-destructive, but busy projects end up with a tangled graph.
How rebase works
A rebase replays your branch’s commits on top of another branch, one at a time:
git checkout feature
git rebase main
The result is a linear history that reads like a story. The catch: it creates new commits with new hashes.
The golden rule of rebasing
Never rebase commits that other people have already pulled. Rewriting shared history forces everyone else into a painful recovery. Rebase your local, un-pushed work freely; leave shared branches alone.
A workflow that works for most teams
- Develop on a feature branch.
- Before opening a pull request,
git rebase mainto get a clean, current branch. - Merge the PR into
mainusing a merge commit (or squash) somainhas a clear record of when features landed.
This gives you tidy feature branches and a truthful main history.
Quick reference
- Cleaning up your own local commits? Rebase.
- Integrating a finished feature into a shared branch? Merge.
- Already pushed and shared the branch? Don’t rebase it.
Master both and you’ll spend far less time fighting your version control.
Tagged
Keep reading
Getting Started with TypeScript: A Practical Guide
TypeScript adds a safety net to JavaScript without slowing you down. Here's how to set it up, the handful of concepts that matter, and how to adopt it gradually.
Understanding the JavaScript Event Loop
Why does JavaScript feel single-threaded yet handle so much at once? The event loop is the answer. Here's a clear mental model with examples you can run.
CI/CD Basics: Automate Deploys with GitHub Actions
Stop deploying by hand. Learn how to set up continuous integration and deployment with GitHub Actions — tests on every push, deploys on every merge.