Git Merge vs Rebase: Differences, Examples, and Best Practices
· Travis Rodgers · 3 min read
When working with Git, you’ll often need to combine changes from one branch into another.
Two of the most common commands are git merge and git rebase. While both achieve a similar goal, they do it in very different ways. Every software engineer should understand the difference, along with the pros and cons of each approach.
What Is Git Merge? (with Example)
git merge
takes the history of two branches and creates a new merge commit (a “third” commit that ties the two histories together). This preserves the full record of how the branches diverged and then rejoined.
# Start on the main branch
git checkout main
# Merge feature branch into main
git merge your-feature-branch
Merge History Example
A---B---C (main)
\
D---E (your-feature-branch)
After merging:
A---B---C-------M (main)
\ /
D---E (your-feature-branch)
M is the new merge commit tying everything together.
✅ Advantages of git merge:
- Preserves complete history
- Useful for teams that want to see exactly when and how branches joined
❌ Disadvantages of git merge:
- Can lead to “messy” histories with lots of merge commits
What Is Git Rebase? (with Example)
git rebase
takes the commits from one branch and replays them on top of another. It does not create a merge commit. Instead, it rewrites commit history so it looks like you developed your work directly on top of the branch you rebased onto.
# Switch to feature branch
git checkout your-feature-branch
# Rebase feature branch onto main
git rebase main
Rebase History Example
Before rebasing:
A---B---C (main)
\
D---E (your-feature-branch)
After rebasing:
A---B---C---D'---E' (your-feature-branch)
D
and E
are new commits replayed on top of C
.
Here’s what actually happened:
- Git created new commits with the same changes as
D
andE
, but their parent is nowC
. - Your feature branch pointer moved to
E
. - The main branch is still at
C
. It hasn’t changed.
So although the history now looks like a straight line, main hasn’t advanced. Your branch is just “rebased on top of main.”
When you merge it back, Git can often do a fast-forward merge (moving main forward with no extra commit). If you want to see what that looks like, check the Fast-Forward Merge section below.
✅ Good for:
- Cleaner, linear history
- Easier to follow when reviewing logs
❌ Downside:
- Rewrites history (can be risky on shared branches)
Fast-Forward Merge (After Rebase)
After rebasing, merging your feature branch back into main is often just a fast-forward:
git checkout main
git merge your-feature-branch # fast-forward merge
Result:
A---B---C---D'---E' (main, your-feature-branch)
Here, Git simply moves the main
pointer forward.
No merge commit is created — the history looks like your work was always built directly on top of main.
Git Merge vs Rebase: Which Should You Use?
- Use merge if you value preserving the true history of your project.
- Use rebase if you want a clean, linear history that’s easy to read.
In practice, many teams use rebase for local cleanup (before pushing) and merge when integrating shared work.
Final Thoughts
Both git merge
and git rebase
are powerful tools. The choice comes down to whether you prioritize historical accuracy (merge) or clean, readable history (rebase).
Understanding when to use each will make your Git workflow smoother, whether you’re working solo or collaborating with a team.
This page may contain affiliate links. Please see my affiliate disclaimer for more info.