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.

Tools Stop 2 of 2 By UniqBlog Desk 30 November 2026 Updated 1 December 2026 6 min read

Soft 3D illustration: two toy track tiles, one where two tracks join into one at a Y junction, the other a straight track with an amber segment lifted to be laid on its end

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 merge adds a merge commit and keeps both lines of history. git rebase replays 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 fetch only downloads. git pull fetches 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 mergegit rebase
History shapeBranches and merge commitsOne straight line
Existing commitsUntouchedReplaced by new commits with new hashes
ConflictsResolved once, in the merge commitResolved per replayed commit
Safe on shared branchesYesNo, unless everyone agrees
git bisect and git logWorks; graph can get noisySimpler to read
Records when work happened in parallelYesNo

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 -i before 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

  1. Is anyone else working on this branch? If yes, merge instead, or agree first.
  2. Commit or stash your changes (rebase.autoStash can do it for you).
  3. Run git fetch and rebase onto origin/main, not a stale local main.
  4. If it goes wrong, git rebase --abort returns you to the start, and git reflog finds the old commits afterwards.
  5. Push with --force-with-lease, with push.useForceIfIncludes turned on.

Sources

UniqBlog Desk · Editors

The editors of UniqBlog. Tested tutorials and tech news for working developers.

Further along the line