Git rebase vs merge: when to use each, with a demo
Git rebase vs merge with a reproducible demo: how each shapes history, when to rebase, the golden rule, safe force-pushes, and git pull vs fetch.
Rebase your own local, unshared commits to keep history linear and readable; merge when you integrate a branch that other people already have. Merge preserves exactly what happened, including when branches diverged. Rebase rewrites your commits as if you had started from the latest main. The one hard rule: do not rebase commits other people may have based work on.
Every command and output below came from a throwaway repository on git 2.53.0. The latest git release is 2.56.1 (2026-10-26); nothing here changed between them.
Key takeaways
git mergeadds a merge commit and keeps both lines of history.git rebasereplays your commits on a new base, which gives them new hashes.- Rebase local work before you push; merge (or let your platform merge) shared branches.
- If you must force-push a rewritten branch, use
--force-with-lease --force-if-includes, never bare--force. git fetchonly downloads.git pullfetches and then integrates. By default it only fast-forwards and stops when branches have diverged.
How do rebase and merge affect history?
Run this script to build the same situation twice, then integrate it once with each command:
#!/usr/bin/env bash
set -euo pipefail
rm -rf /tmp/rebase-demo && mkdir /tmp/rebase-demo && cd /tmp/rebase-demo
export GIT_AUTHOR_NAME=demo GIT_AUTHOR_EMAIL=demo@example.com
export GIT_COMMITTER_NAME=demo GIT_COMMITTER_EMAIL=demo@example.com
git init -q -b main
commit() { echo "$1" >> "$2"; git add "$2"; git commit -qm "$1"; }
commit "init" README.md
git switch -qc feature
commit "feature: add parser" parser.txt
commit "feature: add tests" tests.txt
git switch -q main
commit "main: fix typo" README.md
commit "main: bump deps" deps.txt
git branch feature-merge feature
git branch feature-rebase feature
git switch -q feature-merge
git merge -q --no-edit main
git log --oneline --graph feature-merge
git switch -q feature-rebase
git rebase -q main
git log --oneline --graph feature-rebase
The merge keeps the fork visible and adds one commit that joins the two lines:
* 4de710e Merge branch 'main' into feature-merge
|\
| * 5fefe06 main: bump deps
| * 0ad7b80 main: fix typo
* | 27ae651 feature: add tests
* | 81f3dc2 feature: add parser
|/
* 1d483e7 init
The rebase produces a straight line, with the feature commits on top of main:
* 2d2fb3b feature: add tests
* ee55265 feature: add parser
* 5fefe06 main: bump deps
* 0ad7b80 main: fix typo
* 1d483e7 init
Look at the hashes. feature: add parser was 81f3dc2 and is now ee55265. Rebase does not move commits; it creates new ones with the same changes and a different parent. Your hashes will differ from ours because they include timestamps, but the shapes will match.
git merge | git rebase | |
|---|---|---|
| History shape | Branches and merge commits | One straight line |
| Existing commits | Untouched | Replaced by new commits with new hashes |
| Conflicts | Resolved once, in the merge commit | Resolved per replayed commit |
| Safe on shared branches | Yes | No, unless everyone agrees |
git bisect and git log | Works; graph can get noisy | Simpler to read |
| Records when work happened in parallel | Yes | No |
When should you rebase instead of merge?
Rebase when:
- your feature branch is local, or only you push to it, and you want to catch up with main before opening a pull request;
- you want to tidy commits (squash fixups, reword messages) with
git rebase -ibefore review; - your team requires a linear history, such as "rebase and merge" or "squash and merge" on the hosting platform.
Merge when:
- the branch is shared and others have pulled it;
- you are integrating a long-lived branch, such as a release branch, and the merge point itself is useful history;
- you want to resolve conflicts once instead of once per commit.
A common middle ground: rebase privately while you work, then merge the pull request into main with a merge commit or squash.
What is the golden rule of rebasing?
The Pro Git book states it in one line: "Do not rebase commits that exist outside your repository and that people may have based work on." Its advice for the rest: "rebase local changes before pushing to clean up your work, but never rebase anything that you've pushed somewhere."
If you break the rule, teammates end up with two copies of the same changes under different hashes, and their next pull tries to merge them back together.
How do you force-push a rebased branch safely?
Sometimes you do rewrite a pushed branch, for example your own pull request branch after a rebase. Plain --force overwrites whatever is on the remote. --force-with-lease only overwrites "if the current value of the remote ref is the expected value".
We tested this with a bare repo and two clones. Alice pushed, Bob pushed a fix on top, and Alice, without fetching, amended her commit and tried to push:
== 1. alice: --force-with-lease (alice has not fetched) ==
! [rejected] main -> main (stale info)
Good: Bob's commit is safe. Now the trap. Many editors run git fetch in the background, which updates Alice's view of the remote without her looking at Bob's work:
== 2. after fetch: plain --force-with-lease ==
+ 5a72b60...dbcdfb4 main -> main (forced update) # (--dry-run; would delete Bob's commit)
== 3. after fetch: --force-with-lease --force-if-includes ==
! [rejected] main -> main (remote ref updated since checkout)
--force-if-includes checks that the remote tip is in your branch's reflog, meaning you actually integrated it. Set it once and keep typing only --force-with-lease:
git config --global push.useForceIfIncludes true
git push --force-with-lease
What is the difference between git pull and git fetch?
git fetch downloads new commits and updates remote-tracking branches such as origin/main. It never touches your working branch, so it is always safe. git pull "runs git fetch with the same arguments" and then integrates the upstream branch. Fetch first when you want to look before you integrate:
git fetch
git log --oneline HEAD..origin/main
# d3260d3 teammate: add t
When your branch and the remote have both moved, a plain git pull with no configuration refuses to guess. In our demo it stopped with:
hint: You have divergent branches and need to specify how to reconcile them.
The git docs list fast-forward-only as the default. Choose a behaviour once:
git config --global pull.rebase true # replay local commits on top (linear)
# or
git config --global pull.rebase false # create a merge commit
# or
git config --global pull.ff only # refuse unless it is a fast-forward
With pull.rebase true, the same pull produced a straight line:
* 7f8caa3 you: add y
* d3260d3 teammate: add t
* 16a5a19 init
Checklist before you rebase
- Is anyone else working on this branch? If yes, merge instead, or agree first.
- Commit or stash your changes (
rebase.autoStashcan do it for you). - Run
git fetchand rebase ontoorigin/main, not a stale localmain. - If it goes wrong,
git rebase --abortreturns you to the start, andgit reflogfinds the old commits afterwards. - Push with
--force-with-lease, withpush.useForceIfIncludesturned on.
Sources
Transfer to
UniqBlog Desk · Editors
The editors of UniqBlog. Tested tutorials and tech news for working developers.
Further along the line

Git branching strategy: trunk-based, GitHub Flow or Git Flow
A git branching strategy decides how fast your code reaches main. Compare trunk-based development, GitHub Flow and Git Flow, and pick one in five minutes.

Kubernetes vs Docker: which one do you actually need?
Kubernetes vs Docker is not either-or: Docker builds and runs containers, Kubernetes schedules them across machines. Here is when each layer earns its cost.

Docker Compose volumes: bind mounts, tmpfs and backups
Docker Compose volumes explained with a tested compose file: when to use named volumes, bind mounts or tmpfs, how to name them, and how to back up and restore one.

PostgreSQL vs MySQL in 2026: which database should you pick?
PostgreSQL vs MySQL compared on current versions: JSON, CTEs, window functions, extensions, replication, licensing and cost, with SQL we ran on both.