Full Byte Developer.

Git rebase vs merge

Merge joins histories with a merge commit; rebase replays your commits on another tip for a linear story.

Published here , updated · 4 sources

Git integrates branch work with merge or rebase [1][2]. Merge joins tips with a merge commit; rebase replays your commits onto another tip so history looks linear [1]. The final tree can match; the story in git log --graph will not. Pair with What is Git?.

Quick start: read the graph

bash
git log --oneline --graph -10

Forks with join nodes usually mean merges. A single column often means rebase or fast-forward only. The graph is the story; the tree at HEAD is the code.

Compare merge and rebase on this host

Observed on this host (git 2.43.0, ephemeral repos under /tmp, 2026-12-03). Diverged branches before rebase:

text
* 15d5d30 feature edit
| * e307b90 main moves
|/  
* 49bc315 init

After git checkout feature && git rebase main:

text
* 0221b3a feature edit
* e307b90 main moves
* 49bc315 init

After an explicit merge on a similar two-branch demo:

text
*   c05a6de Merge branch feature
|\  
| * 08f8db2 feat
* | c63e3df mainline
|/  
* 49bc315 init

Rebase moved the feature commit after main's tip with a new commit hash. Merge recorded both tips with a merge commit preserving the fork [1][2].

Reproduce the rebase demo:

bash
rm -rf /tmp/git_rebase_clean && mkdir /tmp/git_rebase_clean && cd /tmp/git_rebase_clean
git init -q && git config user.email '[email protected]' && git config user.name 'Demo'
echo base > README && git add README && git commit -qm init
git branch -M main
git checkout -qb feature
echo feature-only > feat.txt && git add feat.txt && git commit -qm 'feature edit'
git checkout -q main
echo main-only > main.txt && git add main.txt && git commit -qm 'main moves'
git log --oneline --graph --all
git checkout -q feature && git rebase main
git log --oneline --graph --all

Draw the two histories

merge vs rebase Merge (keeps fork) A - B - - - M \ / C ---/ M has two parents [2] Rebase (linear story) A - B - C' C' new SHA, same patch idea do not rebase public commits [1]

Same end tree is possible; the graph and commit IDs tell different stories.

Commands for the safe local sequence

bash
git checkout experiment
git rebase main
git checkout main
git merge experiment

That sequence rebases experiment onto main, then fast-forwards main when the tip is a descendant [1]. If main moved again, repeat rebase before merge.

git pull --rebase applies the same idea when updating your branch from upstream instead of merging upstream into your unpushed work.

Obey the public-history rule

Pro Git in one line: do not rebase commits that exist outside your repository and that people may have based work on [1]. Rewriting shared commits forces teammates into messy re-merges and duplicate-looking history [1].

Safe habit: rebase only local unpublished work; once pushed and shared, prefer merge (or a coordinated force-push policy) [1]. Branch protection can block force-pushes (GitHub branch protection settings) [4].

Force-push after rebase is normal on your own feature branch nobody else fetched. Force-push on main is an incident unless the team planned it.

Resolve conflicts in either path

Both merge and rebase stop on conflicts. Merge leaves you on a merge commit in progress. Rebase leaves you mid-replay with git rebase --continue. In both cases you edit files, git add, then continue [1][2].

Rebase conflicts can appear once per replayed commit; merge conflict often appears once at the join. Either can be tedious on long branches. Use different files on teaching demos if you only want to show graph shape without conflict noise.

Pick for the audience of the log

History-as-record camps keep merges. History-as-story camps rebase private work before publishing [1]. Many teams do both: rebase locally for a clean PR, merge on the shared default branch so the integration is explicit [1].

Squash merge on GitHub is a third shape: many commits become one on main. That erases per-commit story on the default branch while keeping review granularity in the PR.

When merge is the better default

Long-lived shared branches, release branches, or anything multiple people pull should merge unless everyone agrees on rebase rules. Hotfix branches merging back to main and develop often use explicit merge commits for audit trails [2].

Fast-forward merge versus explicit merge

When main has not moved since you branched, git merge feature may fast-forward: no merge commit, just move the pointer. When both tips moved, Git creates a merge commit unless you rebase first [2].

bash
git checkout main
git merge --ff-only feature

--ff-only refuses a merge commit. CI uses it to detect unexpected divergence.

Interactive rebase for cleanup

git rebase -i main lets you squash, reword, or drop commits before publishing. That is still rewriting unpublished history. Once commits are on a shared remote, interactive rebase needs the same coordination as any force-push [1].

Reflog as a safety net

bash
git reflog -5

Reflog still lists pre-rebase tips for a while so you can reset. Entries expire after gc; do not treat them as backup policy [1].

Bisect and archaeology

Merge commits preserve which integration introduced a bug. Pure linear rebase can make git bisect walk a single line but loses the moment two branches met. Neither is universally better; pick history shape to match how your team audits releases [1][2].

Cherry-pick as a third integrator

cherry-pick copies a single commit's patch onto another branch without merging the whole branch. Useful for backports. It creates new SHAs like rebase does [1]. Document cherry-picks in release notes so reviewers know the commit on main is not the same object as the one on develop.

Remote-tracking branches

origin/main moves when you fetch. git rebase origin/main while on a local branch updates your commits against the server tip without merging into main locally. Confusion between local main and origin/main causes many accidental merge commits; git status -sb shows both divergences.

Tags on commits survive rebase only when you move them; otherwise tags still point at old SHAs. Release engineers sometimes tag after merge specifically to avoid chasing rewritten tips [1].

Merge messages and PR titles

Merge commits carry default messages Git generates from branch names. Editing them at merge time helps future git log readers. Rebased histories put commit subjects from the topic branch directly on main; subject quality matters before rebase [1][2].

Common mistakes

Rebasing main after others based work on it. That is the Pro Git stop rule [1].

Treating rebase as a way to avoid conflicts. It moves when conflicts happen, not whether.

Thinking merge and rebase always produce identical trees without checking. They can differ if conflict resolutions differ per step.

Force-pushing a shared feature branch because "rebase is cleaner" without telling collaborators. They need git fetch and reset or rebase onto your new tips.

Using git pull default merge on a linear-preferred team without documenting pull.rebase.

Treating squash merge and rebase as the same. Squash combines into one commit at integrate time; rebase rewrites each commit's parent.

Split diagram on a dark grid. Left: branching teal and tan commit discs that join in interlocking rings. Right: a single vertical stack of teal and tan discs under a teal bar, with side arrows marking the replay.
Merge keeps the fork and join; rebase replays commits into one linear column.

Keep this guide

By Full Byte Developer

Filed under Dev tools, Dev explainers

Questions about this guide

Do merge and rebase produce the same final tree?

Pro Git: the end snapshot can match; the history shape differs. Merge keeps a join commit; rebase replays commits onto another base.

When is rebase dangerous?

Pro Git rule: do not rebase commits that exist outside your repository and that people may have based work on.

What is a common safe pattern?

Rebase local topic commits onto an updated main before you push, then merge or open a PR. Never rewrite shared published history without a team plan.

What is a merge commit?

A commit with two parents that joins two branch tips. git log --graph shows a fork and a join node.

Sources

Next in this stack