
Merge vs Rebase: Same Code, Different History, and the Rule You Can't Break
- كاتب موثّق
محتويات المقال
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
--force takes care of it.
What actually happens
A merge adds one commit and changes nothing else
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
Same snapshot, different commits
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%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
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
# 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# Sara ran: git merge --no-edit main, then git pushgit pull --rebase # Omar replays only his unpushed commit# From ../shop# 423afde..e9367a2 coupons -> origin/coupons# Successfully rebased and updated refs/heads/coupons.git log --oneline --graph# * 0435bf2 Show coupon errors# * e9367a2 Merge branch 'main' into coupons# |\# | * 65f5f84 Add shipping to total# * | 423afde Validate coupon codes# * | 42d4aa8 Add coupon field# |/# * fc07af2 Add checkoutgit push# To ../shop.git# e9367a2..0435bf2 coupons -> couponsgit 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.
How to apply it in your project
Agree on which branches are shared main, and any branch that more than one person pushes to, is shared. Write that down, and block force pushes tomainin your Git host's branch settings.Integrate shared branches with merge To bring maininto a shared feature branch, rungit merge main. It adds one commit and rewrites nothing that anyone else holds.Rebase only commits that haven't left your machine Use git pull --rebase, orgit rebase -ito tidy up before your first push. Once a commit is pushed and someone may have pulled it, leave it alone.If you must rewrite a pushed branch, use the lease For 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-leaseto--force.
التعليقات (0)