Dev tools

Git Rebase vs Merge: Which One Should You Use?

Git rebase vs merge explained with diagrams and commands: how each works, when to use which, squash merges, the golden rule, conflicts and team workflows.

By OmniAI Editorial · Nov 22, 2026 · 13 min read

Git rebase vs merge is a choice about history. Both bring changes from one branch into another. git merge combines branches by creating a merge commit that ties the two histories together, keeping every commit exactly as it was. git rebase moves your commits so they sit on top of the other branch, rewriting them into a straight, linear history. Merge is safer and preserves context; rebase gives a cleaner log but rewrites commits, so never rebase commits that others have already pulled. A common team practice is to rebase your own local feature branch to keep it current, then merge it into the main branch through a pull request.

This guide explains the difference between git merge and rebase with diagrams, a git rebase vs merge with example walkthrough, fast-forward and squash merges, interactive rebase, the golden rule, how conflicts differ, and which workflow fits which team.

Git Rebase vs Merge at a Glance

git mergegit rebase
What it doesJoins two histories with a merge commit (or fast-forwards)Replays your commits on top of another base
History shapeBranching graph that shows when work diverged and joinedLinear, as if work happened in sequence
Rewrites commits?No; existing commits keep their hashesYes; rebased commits get new hashes
Safe on shared branches?YesNo, unless everyone coordinates; it rewrites published history
ConflictsResolved once, in the merge commitResolved commit by commit as each is replayed
TraceabilityShows exactly when a branch was integratedIntegration points disappear; history reads as one line
UndoRevert the merge commitHarder; use the reflog to recover the old commits
Extra powers--no-ff, --squashInteractive rebase: reorder, edit, squash, drop commits
Typical useIntegrating feature branches into main; shared branchesUpdating a private feature branch; cleaning up commits before review

How Git Merge Works

git merge takes the commits from another branch and integrates them into your current branch.

git switch main
git merge feature  # bring feature's commits into main

Git finds the merge base, the last commit both branches share, and combines the changes made on each side since then. What happens next depends on the history:

  • Fast-forward merge: if main hasn't moved since feature branched off, Git simply moves the main pointer forward to the tip of feature. No new commit is created, and history stays linear.
  • Three-way merge: if both branches have new commits, Git creates a merge commit with two parents, recording that the histories joined.

You can control this behaviour with flags:

git merge --no-ff feature    # always create a merge commit, even if a fast-forward is possible
git merge --ff-only feature  # refuse unless a fast-forward is possible
git merge --squash feature   # stage all of feature's changes as one change; you then commit

Merging never changes existing commits. That's its main safety property: anyone who already has main or feature can keep working without surprises.

How Git Rebase Works

git rebase takes the commits that are on your branch but not on the target, sets them aside, moves your branch to the tip of the target, and replays each commit on top, one at a time.

git switch feature
git rebase main  # replay feature's commits on top of the latest main

Because each replayed commit has a new parent, it becomes a new commit with a new hash, even if the changes are identical. The old commits still exist for a while (you can find them with git reflog), but your branch now points to the rewritten ones.

The result is a history that looks as if you started your work from the latest main. When you later merge feature into main, it can fast-forward, producing a straight line.

Git Rebase vs Merge With Example

Here's a git rebase vs merge with example. You branched feature from main at commit B and made commits D and E. Meanwhile, a teammate pushed C to main.

Starting point:

          D---E  feature
         /
    A---B---C  main

Option 1: merge `main` into `feature` (or `feature` into `main`). Git creates a merge commit M with two parents:

          D---E---M  feature
         /       /
    A---B-------C  main

History is preserved exactly; anyone can see that D and E were written before C existed. The cost is an extra commit and a graph that grows tangled when many branches merge back and forth.

Option 2: rebase `feature` onto `main`. Git replays D and E on top of C as new commits D' and E':

                  D'--E'  feature
                 /
    A---B---C  main

Now main can fast-forward to E', giving A, B, C, D', E' in a straight line. The history is easier to read and bisect, but D and E were rewritten: if a teammate had pulled the old feature branch, their copy now conflicts with yours.

On the left a branch of grey commit circles arching above an ink line and rejoining it at a larger circle; on the right a straight line of circles where the last two are light, the final one orange, under a dotted ink arc.
Merge joins histories with a merge commit; rebase replays your commits on top for a straight line.

The Difference Between Git Merge and Rebase

The difference between git merge and rebase comes down to five practical points:

  1. History truth vs history readability. Merge records what actually happened, including parallel work. Rebase records a tidier story of what the change set is.
  2. Commit identity. Merge keeps commit hashes; rebase creates new ones. Anything that refers to old hashes (open discussions, other branches, deployment records) can break after a rebase.
  3. Conflict handling. Merge shows all conflicts at once in one merge commit. Rebase stops at each commit that conflicts, which can mean resolving similar conflicts several times on a long branch (git rerere can remember resolutions).
  4. Safety on shared branches. Merge is always safe to push. Rebasing a branch others use forces them to reconcile rewritten history.
  5. Debugging. A linear history makes git bisect and git log easier to follow; merge commits preserve the context of how features came together.

Git Rebase vs Merge vs Squash

Squash merging is the third option in most platforms, so git rebase vs merge vs squash is the real choice many teams make. A squash merge takes every commit on a branch and combines them into a single commit on the target.

MethodHistory on mainKeeps individual commits?Best for
Merge commitBranch commits plus a merge commitYesTeams that value exact history and feature-level grouping
Rebase and mergeBranch commits replayed linearly, no merge commitYes, rewrittenTeams with clean, meaningful commits who want a linear log
Squash and mergeOne commit per pull requestNo; collapsed into oneTeams whose branch commits are messy ("fix typo", "wip")

GitHub's pull request merge button offers exactly these three: "Create a merge commit", "Squash and merge" and "Rebase and merge", and repository settings can allow or disallow each one. GitLab and Bitbucket offer the same options under similar names.

Squash merges give a very clean main branch, one commit per change, which is easy to revert. The trade-off is lost detail: if a pull request contained several logical steps, they become one, and the original branch commits aren't ancestors of main, so tools that check "is this branch merged?" can be confused.

When to Rebase vs Merge

Use rebase when:

  • you're updating your own feature branch with the latest main before opening or updating a pull request,
  • you want to clean up local commits (squash fixups, reword messages) before others review them, or
  • your team wants a linear history and agrees on the workflow.

Use merge when:

  • you're integrating a finished branch into a shared branch such as main or release,
  • other people are working on the same branch,
  • you want to preserve exactly when and how work was combined, or
  • you're unsure; merging can't damage anyone else's work.

A practical default for most teams: rebase locally, merge (or squash) through pull requests, and protect main so nobody rewrites it.

Is Rebase Better Than Merge?

Is rebase better than merge? Neither is better in general; each optimises for something different. Rebase optimises for a readable, linear history; merge optimises for accuracy and safety. Large open-source projects show both styles: some maintain strictly linear histories through rebasing or patch application, while others rely on merge commits to group and trace contributions. What matters is that the team picks one policy, configures the platform to enforce it, and applies it consistently.

The Golden Rule of Rebasing

Never rebase commits that exist outside your local repository and that others may have based work on. Rebasing rewrites those commits. If a teammate has the old versions, their next pull will try to combine two different histories of the same work, leading to duplicated commits and confusing conflicts.

If you must update a pushed branch after rebasing (for example, your own pull request branch that nobody else uses), push with:

git push --force-with-lease  # overwrite the remote branch only if nobody else pushed in the meantime

--force-with-lease is safer than --force because it refuses to overwrite commits on the remote that you haven't seen.

Interactive Rebase: Cleaning Up Before Review

Interactive rebase lets you edit a series of commits before sharing them:

git rebase -i HEAD~4  # edit the last four commits

Git opens a list of commits with an action for each:

  • pick: keep the commit as is.
  • reword: change the commit message.
  • edit: stop to amend the commit's contents.
  • squash: combine with the previous commit and merge the messages.
  • fixup: combine with the previous commit and discard this message.
  • drop: remove the commit.

A tidy workflow: commit small fixes as git commit --fixup <commit> while working, then run git rebase -i --autosquash main to fold them into the right commits automatically. If you work with stacked branches that depend on each other, git rebase --update-refs moves the dependent branch pointers along with the rebase.

A light list panel with four grey rows, each starting with a small ink chevron, and an orange bracket joining the second and third rows.
Interactive rebase lets you reorder, reword and squash commits before anyone else sees them.

Rebase vs Merge When Pulling

git pull fetches changes and then integrates them. By default it merges, which can create small "Merge branch 'main' of ..." commits whenever you and a teammate both committed. Many developers prefer:

git pull --rebase             # replay your local commits on top of what you fetched
git config --global pull.rebase true  # make that the default

That keeps local history linear without affecting anyone else, because your local, unpushed commits are the only ones being rewritten. If you prefer to never create accidental merge commits, git config --global pull.ff only makes git pull refuse unless it can fast-forward, so you decide explicitly.

Conflicts: Merge vs Rebase

Both can stop with conflicts; the workflow differs.

During a merge: Git marks all conflicting files at once. Fix them, git add the files, and run git commit (or git merge --continue). To give up, run git merge --abort.

During a rebase: Git stops at the first commit that conflicts. Fix it, git add the files, then git rebase --continue to move to the next commit. Repeat until done. git rebase --skip drops the current commit, and git rebase --abort returns to where you started.

Long-running branches with many commits make rebase conflicts painful, because you may fix the same area repeatedly. Integrating main more often keeps conflicts small whichever command you use. Our guide to fixing a git merge conflict walks through reading conflict markers and resolving them in an editor.

Team styleFeature branchesIntegration into mainSettings to enable
Small team, wants linear historyRebase on main before reviewRebase and merge, or squashAllow only rebase or squash merges; require branches to be up to date
Team that values exact historyMerge main into the branch when neededMerge commit with --no-ffAllow merge commits; protect main from force pushes
Open-source project with many contributorsContributors rebase their pull requestsSquash and merge for small changesRequire linear history if using squash or rebase
Trunk-based team with short-lived branchesBranches live hours, rebased as neededSquash or rebase merges, often through a merge queueMerge queue, required checks, protected main

If you juggle several branches at once, git worktree lets you check out each in its own directory so you can rebase one without stashing work in another; our git worktree guide shows how.

Common Mistakes

  • Rebasing a shared branch and force-pushing over teammates' work.
  • Using `--force` instead of `--force-with-lease`.
  • Rebasing a very long-lived branch in one go and fighting the same conflict across dozens of commits. Rebase or merge main in regularly instead.
  • Losing work after a bad rebase without knowing about git reflog, which records where your branch pointed before, so you can reset back.
  • Squashing away meaningful history on large pull requests where separate commits explained separate decisions.
  • Mixing policies, so the history is half merge commits and half rebased lines with no pattern.

FAQ

Should I rebase or merge before a pull request?

Rebasing your feature branch onto the latest main before opening or updating a pull request is common, because it makes the diff clean and lets the merge fast-forward. Only do it if nobody else is working on your branch.

Does rebase lose commits?

No, not normally. Rebase creates new versions of your commits and moves your branch to them. The originals remain reachable through git reflog for a while, so you can recover if something goes wrong.

What is a fast-forward merge?

When the target branch hasn't changed since your branch split off, Git can simply move the target's pointer to your latest commit. No merge commit is created and history stays linear.

Is squash merge the same as rebase?

No. Rebase keeps each of your commits (rewritten onto a new base); squash merge combines all of them into one new commit on the target branch.

How do I undo a rebase?

Find the commit your branch pointed to before the rebase with git reflog, then run git reset --hard <that-commit>. Make sure you have no uncommitted work you want to keep first.

All articles

Read next

Language

EnglishEnglish · en TürkçeTurkish · tr DeutschGerman · de العربيةArabic · ar

Account

Enter your email. We’ll send you a sign-in link and a 6-digit code. No password needed.

Sign in

Theme

Language

Show our email address

To keep spam out of our inbox, please confirm your own email first. We will send you a 6-digit code.

We send codes only to these providers: gmail.com, googlemail.com, outlook.com, hotmail.com, live.com, msn.com, proton.me, protonmail.com, pm.me, icloud.com, me.com, mac.com, yahoo.com, aol.com, gmx.de, gmx.net, web.de, t-online.de, yandex.com, omniai.blog