Merge vs Rebase
Merge joins histories. Rebase recreates commits on a different base.
Introduction
git merge and git rebase both bring a feature branch up to date or get it ready to land. Merge keeps the commits you have and records where two histories met. Rebase replaces a line of commits so it starts somewhere else.
The choice shapes your history and how you work with others. It has no bearing on whether the final files are right. The short rule is to rebase private work and merge shared work.
Understanding the Concept
Every example below starts from the same graph. You branched feature from main at commit B. Since then you added D and E, and someone pushed C to main.
D---E feature
/
A---B---C main
A merge commit has two or more parents. Running git merge main on feature creates a new commit M whose first parent is E and whose second parent is C. Every existing commit keeps its object ID.
D---E---M feature
/ /
A---B-------C main
Definition
Merge Commit
A commit with two or more parents that records the point where separate lines of history were combined.
A rebase computes the patch each commit introduces, applies those patches to a new base in order, and moves the branch ref to the result. Running git rebase main on feature gives new commits D' and E'. Their object IDs change because their parents change. The old D and E stay reachable through the reflog until garbage collection removes them.
D'--E' feature
/
A---B---C main
Definition
Fast-forward
A ref update that moves a branch pointer to a descendant commit without creating a merge commit.
After the rebase, main is an ancestor of feature. Running git merge feature on main now fast-forwards. Git moves the main ref to E' and creates no merge commit. Use --no-ff when the project wants an explicit integration commit even then.
A squash merge takes a third path. git merge --squash feature stages the combined change of D and E without committing. The git commit that follows creates one ordinary commit S with a single parent. Git records nothing that ties S to the feature branch.
A---B---C---S main
Definition
Squash Merge
An integration that applies a branch's combined changes as one new single-parent commit on the target branch.
Merge and Rebase Side by Side
| Merge | Rebase | Squash merge | |
|---|---|---|---|
| Existing commits | Preserved | Replaced by new copies | Replaced by one new commit |
| History shape | Keeps branch topology | Linear | Linear |
| Commits created in this example | One merge commit | New copies of the two feature commits | One commit after git commit |
| Safe on shared branches | Yes | Only with agreement | Yes on the target branch |
| Conflicts | Resolved once, in the merge result | Can stop at each replayed commit | Resolved once |
| Push afterwards | Normal push | Force push if already pushed | Normal push |
| Commit boundaries kept | Yes | Yes | No |
When to Use Which
Use merge when:
- other people have pulled or built on the branch
- you want the graph to show when a branch landed, readable with
git log --first-parent - the branch is long-lived, such as a release branch taking fixes from
main
Use rebase when:
- the branch is yours alone and not yet shared
- you want to update a feature branch from
mainwithout adding merge commits to it - you are cleaning up commits with
git rebase -ibefore review, as in Git Squash Commits
Use squash merge when each pull request should land as one commit on main. The cost is that intermediate commits vanish from the target branch history.
The Golden Rule of Rebasing
Do not rebase commits that exist outside your repository and that others may have built on. The git rebase documentation warns that rebasing a branch others have based work on forces everyone downstream to fix their history by hand.
The mechanism is object identity. A colleague's branch points at your old E. After your rebase, E' replaces E on your branch. When they pull, Git sees two versions of the same change and either duplicates the commits or raises conflicts. The rule bends only when everyone who uses the branch agrees to the rewrite and knows how to recover.
Handling Conflicts
If a merge reports conflicts, resolve them, stage the files with git add, then run git merge --continue. To cancel the merge, run git merge --abort.
A rebase replays commits one at a time, so it can stop at each one. Fix the files, git add them, then run git rebase --continue. Use git rebase --skip to drop the current commit, or git rebase --abort to return the branch to where it started. A conflict on the same lines in five commits can stop you five times.
Two settings cut that repetition. git rerere records how you resolved a conflict and reapplies the resolution when the same conflict returns. zdiff3 shows the merge base in conflict markers, so you can see what each side changed. After a large rebase, git range-diff compares the old and new series to check the rewrite still says what you meant.
Pushing After a Rebase
A rebased branch no longer contains the commits on its remote copy, so a plain git push is rejected as a non-fast-forward. Use --force-with-lease instead of --force:
git fetch origin
git switch feature/my-work
git rebase origin/main
git push --force-with-lease origin feature/my-work
--force-with-lease refuses the push if the remote branch moved since you last fetched it. That stops you from deleting a commit a reviewer pushed while you were rebasing. A merge never needs a force push, because the new history contains the old one.
Applying It in Practice
Merge main into a shared feature branch:
git fetch origin
git switch feature/my-work
git merge origin/main
Integrate a branch with an explicit merge commit:
git switch main
git merge --no-ff feature/my-work
Squash a branch into one commit on main:
git switch main
git merge --squash feature/my-work
git commit -m "Add user authentication"
Hosting platforms also offer rebase-and-merge. That option replays pull-request commits onto the target branch and usually creates new object IDs. Check the platform's exact behavior before making policy depend on it.
Stacked PRs and Parallel Agent Branches
In a stack of dependent branches, each branch is based on the one below it. When the bottom branch changes after review, every branch above it needs a rebase onto the new tip. Merging the lower branch upward also works, but it fills each pull request with merge commits that reviewers then read past.
Squash merges make stacks harder. When the bottom pull request lands as one squashed commit, the branch above still holds the original commits. A plain git rebase main tries to replay them again and conflicts with their squashed copy. Use git rebase --onto main <old-base> <branch> to replay only the commits that belong to the upper branch. Before the bottom lands, git rebase --update-refs moves every branch in the stack in one pass.
Parallel agent branches have the same shape at a larger scale. Several agents branch from main, and each lands at a different time. Every landing moves main, and the remaining branches fall behind. Rebasing each one onto the new main keeps its diff small and reviewable. Auto-rebasing AI branches covers how to do that without breaking work in progress. When you need one fix from another agent's branch, cherry-pick that one commit and leave the branch alone.
Engineering Considerations
git bisect works on straight and merged histories alike. A merge commit can muddy a regression when neither parent reproduces it on its own, and a squash can bury the step that caused the bug. What makes history useful is commits that build and stay focused, not the shape of the graph.
git blame also works across merges. Options such as -M and -C help detect moved or copied lines. Rebase does not make blame more accurate by itself.
Scaling and Operations
Set one integration policy for each protected branch. Common choices are:
- merge commits for an explicit first-parent release history
- squash merges for one target-branch commit per pull request
- rebase-and-merge for preserving individual pull-request commits in a linear history
Each option throws away something different. Merge commits keep the shape. Squash keeps the combined change but loses where each commit began and ended. Rebase-and-merge keeps those boundaries but gives every commit a new identity.
Block force pushes on shared branches. If feature branches can be rewritten, say so in the contribution guide, so reviewers know whether it is safe to build on one. Run the full test suite after you resolve conflicts, whichever strategy you picked.
Next Steps
- Cherry-pick vs Rebase: distinguish selected patch copying from branch rewriting
- What is Git Rerere?: reuse a conflict resolution when the same conflict returns
- Merge Conflicts with Coding Agents: resolve conflicts across parallel agent branches
- Stacked PR Workflow: apply rebasing to a pull-request stack
- Parallel Development Workflow: combine integration policy with concurrent branch work