Skip to main content

Git rebase vs. git merge

· 7 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

Is rebase better than merge?

Neither rebase nor merge is inherently "better"; they are different tools used for different purposes in Git. The choice between them depends on your workflow, your team's preferences, and whether you want a clean, linear history or an accurate, chronological record of events (3).


The Case for rebase: The Clean History

git rebase is often preferred when the goal is to maintain a clean, linear, and easy-to-read commit history. It makes it look as if a feature was developed directly on top of the main branch.

Why it's better for a clean history:

  • Avoids "Merge Commits": When you use git merge, it creates a new commit that combines the histories of two branches. This can lead to a messy commit graph with many merge commits, making it harder to follow the main line of development.
  • A "Falsely" Simple History: rebase rewrites history to make it look like a feature branch's commits were created after the latest commits on the main branch. This creates a linear history, which is easier to navigate with commands like git log and git bisect.

Real-Life Example: The "Squash and Rebase" Workflow

Imagine you're working on a feature and have made many small, incremental commits: "fix typo," "add new function," "another small change."

Before creating a pull request, you want to present a clean, concise history to your reviewers. You can use an interactive rebase (git rebase -i) to squash these multiple commits into one single, meaningful commit.

# Start an interactive rebase on the last few commits
git rebase -i HEAD~5

# You'll be presented with a text editor to squash commits
pick 1a2b3c4 Add new feature
s 5d6e7f8 Fix typo
s 8g9h0i1 Another small change
...

# The result is one clean commit: "feat: Add new user authentication feature"

After squashing, you can rebase this single commit onto main, and then force-push it to your remote feature branch. This presents a single, polished commit for your code review, rather than a dozen small, messy ones. This is especially useful in a "topic branch" workflow where you want to keep the main branch's history pristine.


The Case for merge: The Accurate History

git merge is preferred when the priority is to preserve an accurate and chronological record of the project's development. It maintains the true history of when and how branches diverged and came back together.

Why it's better for an accurate history:

  • Non-Destructive: git merge doesn't rewrite history. It simply adds a new commit to the branch, which is a safer operation. This is especially important for public or shared branches where rewriting history is a cardinal sin (3).
  • Maintains Context: The merge commit explicitly records the fact that a branch was merged at a specific point in time. This provides valuable context for debugging, allowing you to see exactly when and where a feature was integrated.

Real-Life Example: The "Team Collaboration" Workflow

Consider a scenario where multiple developers are collaborating on a long-running feature branch. This branch is pushed to the remote, and everyone pulls from it.

If a new developer joins the team and pulls the branch, they need to see the history of the branch as it actually happened. If one developer were to rebase this shared branch, it would break the workflow for everyone else, leading to a confusing and painful recovery process (2).

Instead, the team uses git merge to integrate changes from main into the shared feature branch.

# While on the shared feature branch
git fetch origin
git merge origin/main
# A new merge commit is created, recording the integration

This creates a new merge commit, preserving the history for everyone. The merge operation is non-destructive and doesn't require a force-push, making it ideal for collaborative environments.


Summary: Rebase vs. Merge

Featuregit rebasegit merge
HistoryClean, linear, and rewrittenAccurate, chronological, and preserves merge points
SafetyRisky on shared branchesSafe for all branches
Use CaseLocal, private feature branchesPublic, shared, or long-running branches
Commit GraphFlat, no merge commitsCan be cluttered with merge commits

In conclusion, rebase is a powerful tool for cleaning up your personal, local history before sharing it, but merge is the safer and more reliable option for integrating work on shared branches (1).


Sources

  1. Git Merge vs. Rebase
  2. Git Push
  3. The Golden Rule of Rebasing