We are going to explain the difference between git merge and git rebase in a clear and concise way.
Both git merge and git rebase are used to integrate changes from one branch into another, but they do so in different ways.
Git Merge:
When you merge a branch (say, feature) into another branch (say, main), Git creates a new commit that combines the changes from both branches.
This results in a merge commit, which has two parent commits (one from each branch).
The history remains exactly as it happened, with a clear record of when branches were merged.
It is non-destructive: the existing branches are not altered.
The project history can become cluttered with many merge commits, especially if there are many branches and frequent merges.
Example:
main: A---B---C
feature: D---E
After merging feature into main (with a fast-forward if possible, but if not, a merge commit is created):
main: A---B---C---M
\
D---E (feature branch is not modified, but now main includes both)
If there are divergent changes (i.e., both branches have new commits after the common ancestor), then a merge commit (M) is created.
Git Rebase:
Rebase moves the entire feature branch to begin on the tip of the main branch.
It does this by creating new commits for each commit in the feature branch (so the commit hashes change).
The result is a linear history: the feature branch's commits appear as if they were made directly on top of the main branch.
It rewrites the commit history of the feature branch.
It can make the history cleaner and easier to follow (no merge commits), but it should be used with caution, especially on shared branches.
Example:
main: A---B---C
feature: D---E
After rebasing feature onto main (assuming main has moved forward to C, and feature is based on B):
First, we move the base of feature to C, then replay D and E on top:
main: A---B---C
feature: D'---E'
Now the feature branch is a straight line from C, and the original D and E are gone (but still in the reflog until garbage collected).
Key Differences:
History:
Merge: preserves the exact history, including the existence of the feature branch and the merge point.
Rebase: creates a linear history by moving the feature branch's base.
Commit Hashes:
Merge: the existing commits remain the same (only a new merge commit is added).
Rebase: creates new commits (with new hashes) for each rebased commit.
Use Case:
Merge: best for public branches (like merging a feature branch into main) because it doesn't rewrite history.
Rebase: best for cleaning up local, private branches before sharing (so that the history is clean when merged).
Conflict Resolution:
Merge: conflicts are resolved once, at the merge commit.
Rebase: conflicts may have to be resolved for each commit that is rebased (though Git will pause at each conflicting commit).
Safety:
Merge: safe for shared branches because it doesn't change existing commits.
Rebase: unsafe for shared branches because it rewrites history, which can cause problems for others who have based work on the original commits.
When to use which?
Use git merge when you want to combine changes from two branches and preserve the history exactly as it happened. This is the default and safest for shared branches.
Use git rebase when you want to incorporate upstream changes into your feature branch (to keep your branch up to date with main) and when you want a clean, linear history. But only do this on branches that you are the only one working on.
A common workflow is:
Start a feature branch from main.
Make commits on the feature branch.
Periodically rebase the feature branch onto main to keep it up to date (if the branch is private).
Then, when the feature is ready, merge the feature branch into main (with a merge commit, or if you want a linear history on main, you can do a fast-forward merge or even rebase the feature branch onto main and then fast-forward main).
However, note that if you rebase your feature branch onto main and then merge, you might get a fast-forward merge (if main hasn't moved since the rebase) and the history will be linear. But if others have been working on the feature branch with you, rebasing is dangerous.
Visual Example:
Let's say we have:
code
main: A---B---C
feature: D---E
Merge:
code
main: A---B---C---M
\ \
D---E
Rebase:
code
First, we are on feature branch and run `git rebase main`:
main: A---B---C
feature: D'---E'
Then, if we switch to main and merge feature (with fast-forward because feature is ahead of main by two commits and main hasn't moved):
main: A---B---C---D'---E'
But note: the original D and E are still in the reflog of the feature branch until garbage collected.
Conclusion:
Both are useful tools, but they serve slightly different purposes and have different implications for the project history. The choice between them depends on the workflow and whether the branch is shared.
Remember: never rebase a branch that others are using (unless you coordinate and they are willing to adjust their work). For shared branches, use merge.
Also, note that there is an option to do a git merge --no-ff (no fast-forward) which always creates a merge commit, even if a fast-forward is possible. This can be used to explicitly show that a feature branch was merged.
And there is git pull --rebase which is a common way to rebase your current branch on top of the upstream branch when pulling (instead of creating a merge commit).
It's important to understand the history you want to present and the collaborative nature of your project when choosing between merge and rebase.
Git Merge vs Git Rebase
Both git merge and git rebase integrate changes from one branch into another, but they handle history differently:
Git Merge
What it does: Creates a new merge commit that combines histories of both branches.
History: Preserves the complete chronology with a visible merge point (non-linear history).
Commit hashes: Original commits remain unchanged; only adds a new merge commit.
Safe for shared branches: Since it doesn't rewrite existing commits, it's safe for public branches.
Example workflow:
bash
git checkout main
git merge feature-branch
Result:
code
main: A---B---C---M
\ \
feature: D---E
(M = merge commit)
Git Rebase
What it does: Replays commits from the current branch onto the tip of the target branch, rewriting history.
History: Creates a linear history by moving the entire branch base.
Commit hashes: Creates new commits (different hashes) for rebased changes.
Dangerous for shared branches: Rewriting published history can disrupt collaborators.
Example workflow:
bash
git checkout feature-branch
git rebase main
Result:
code
main: A---B---C
\
feature: D'---E' (D' and E' are new commits)
Key Differences
Feature
git merge
git rebase
History shape
Non-linear (with merges)
Linear
Commit hashes
Unchanged (except merge)
Rewritten (new commits)
Conflict handling
Single conflict resolution
Per-commit conflicts
Traceability
Preserves branch context
Loses original branch structure
Safety
Safe for shared branches
Only for private branches
Audit trail
Shows when branches merged
Hides temporary branch
When to Use Which
Use git merge when:
Working on shared/public branches (e.g., merging a feature into main).
You want to preserve exact historical context.
-Collaborating with others on the same branch.
Use git rebase when:
Cleaning up local feature branches before sharing.