الانتقال إلى المحتوى
DevOps والـ CloudGit· 7 دقائق للقراءة

Merge vs Rebase: Same Code, Different History, and the Rule You Can't Break

Both can leave you with the same files. Rebase makes new commits, so never use it on commits someone else already has.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
محتويات المقال

Sara and Omar share a branch called coupons. Before review, Sara wants a tidy history, so she rebases the branch onto main. Git rejects her push, so she adds --force. The next morning Omar pulls, and his log shows "Add coupon field" twice, "Validate coupon codes" twice, and a merge commit he never meant to make. No code is missing, yet the branch now tells the same story twice, and every reviewer asks which copy is the real one.

The common misconception

The belief: rebase is just a cleaner merge. It produces the same code with a straighter history, so you can use it on any branch, shared ones included. If the push is rejected afterwards, --force takes care of it.

The belief has good support. Pro Git itself says of the two: "There is no difference in the end product of the integration, but rebasing makes for a cleaner history." A linear log is easier to scan, which is why some teams ask for one before review. As far as the files go, the belief is exactly right.

What it misses is that a rebase doesn't move your commits. It replaces them.

What actually happens

A merge adds one commit and changes nothing else

The git merge docs describe it: Git takes the other branch's changes since the two diverged and records the result "in a new commit along with the names of the two parent commits." That is the only new object. Every existing commit keeps its ID, so anyone who already has them is unaffected.

A rebase writes new commits

Pro Git explains the mechanics: rebase works by "getting the diff introduced by each commit of the branch you're on, saving those diffs to temporary files, resetting the current branch to the same commit as the branch you are rebasing onto, and finally applying each change in turn." Each replayed change becomes a new with a new parent, and because a commit's ID is a hash of its content, parent included, it gets a new ID. In the book's words: "When you rebase stuff, you're abandoning existing commits and creating new ones that are similar but different."

Same snapshot, different commits

Here's Sara's branch integrated both ways, on Git 2.54:

Bash terminal
git log --oneline main..coupons# e8ea870 Validate coupon codes# 8289bcb Add coupon fieldgit switch -q -c try-merge coupons && git merge -q --no-edit maingit switch -q -c try-rebase coupons && git rebase -q maingit log --no-walk --format='%h %t %s' try-merge try-rebase# 834ad14 0bf12b3 Validate coupon codes# c923a93 0bf12b3 Merge branch 'main' into try-merge

The second column, %t, is the tree: the snapshot of every file. It's 0bf12b3 for both, so the code is identical. The first column isn't: "Validate coupon codes" was e8ea870 and is now 834ad14. Pro Git puts it exactly: "it's only the history that is different."

Shared commits make the difference matter

New IDs are harmless while nobody else has the old ones. Once a teammate has pulled them and built on them, their clone still holds the originals, and Git sees the rewritten copies as different commits. The git rebase docs are blunt: "Rebasing (or any other form of rewriting) a branch that others have based work on is a bad idea: anyone downstream of it is forced to manually fix their history." Pro Git reduces it to one rule: "Do not rebase commits that exist outside your repository and that people may have based work on."

The fix: merge what's shared, rebase what's yours

Both sessions are Omar's clone, after Sara updated the shared branch in two different ways:

Bash terminal
# Sara ran: git rebase main, then git push --forcegit pull --no-rebase   # Omar, with one unpushed commit# From ../shop#  + b3086e8...d707e50 coupons    -> origin/coupons  (forced update)# Merge made by the 'ort' strategy.git log --oneline --graph# *   f7d87bc Merge branch 'coupons' of ../shop into coupons# |\# | * d707e50 Validate coupon codes# | * 9356d40 Add coupon field# | * 4680f97 Add shipping to total# * | 14e1e70 Show coupon errors# * | b3086e8 Validate coupon codes# * | 2abf6f9 Add coupon field# |/# * 86434d1 Add checkout

In the first tab, Sara's plain git push had been rejected as non-fast-forward before she forced it. Omar's pull fetched the rewritten branch, found that his history and the remote's had diverged, and merged them, so each of Sara's commits now appears twice: b3086e8 and d707e50 are both "Validate coupon codes". In the second tab, Sara merged main instead, so her commits kept their IDs and a normal push worked. Omar replayed only his own unpushed commit with git pull --rebase, and his push needed no force either.

MergeRebase
What it createsOne merge commit with two parentsNew copies of your commits, with new IDs
Existing commitsUntouchedReplaced by the copies
Resulting filesThe same snapshotThe same snapshot
History shapeShows where work diverged and joinedLinear, as if written in sequence
Pushing afterwardsA normal pushA force push if the old commits were pushed

How to apply it in your project

  1. Agree on which branches are sharedmain, and any branch that more than one person pushes to, is shared. Write that down, and block force pushes to main in your Git host's branch settings.
  2. Integrate shared branches with mergeTo bring main into a shared feature branch, run git merge main. It adds one commit and rewrites nothing that anyone else holds.
  3. Rebase only commits that haven't left your machineUse git pull --rebase, or git rebase -i to tidy up before your first push. Once a commit is pushed and someone may have pulled it, leave it alone.
  4. If you must rewrite a pushed branch, use the leaseFor a branch only you push to, push the rewrite with git push --force-with-lease, never plain --force. It refuses when the remote has commits you haven't fetched.

Key takeaways

  • Merge and rebase can produce the same files; only the history differs.
  • A merge adds one commit with two parents and leaves every existing commit untouched.
  • A rebase replaces your commits with new copies that have new IDs.
  • Never rebase commits that exist outside your repository and that people may have based work on.
  • Rebase local work before pushing, merge shared branches, and prefer --force-with-lease to --force.

📚 المصادر

  1. Git Branching: Rebasing — Pro Git
  2. git-rebase — Git reference
  3. git-merge — Git reference
  4. git-push — Git reference

التعليقات (0)