git-advanced-merging
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAdvanced Git Merging
Git高级合并
Conflict Marker Style
冲突标记样式
Default hides the common ancestor. Show it:
bash
git config --global merge.conflictStyle zdiff3 # git 2.35+; else diff3
git checkout --conflict=diff3 path # re-render existing conflictMarkers become — resolution is far easier.
<<<ours / ||| base / === / theirs >>>默认会隐藏共同祖先。如需显示:
bash
git config --global merge.conflictStyle zdiff3 # git 2.35+; else diff3
git checkout --conflict=diff3 path # 重新渲染现有冲突标记会变为——冲突解决会容易得多。
<<<ours / ||| base / === / theirs >>>Side-Picking
选择分支版本
bash
git checkout --ours path
git checkout --theirs path
git add pathDuring rebase, ours/theirs swap: "theirs" = the commits being replayed.
bash
git checkout --ours path
git checkout --theirs path
git add path在rebase(变基)过程中,ours/theirs会互换:"theirs"指的是正在重放的提交。
Strategy (-s
) vs Strategy Option (-X
)
-s-X策略(-s
)与策略选项(-X
)
-s-X- Strategy () picks the algorithm:
-s(default),ort,recursive,resolve,octopus,ours.subtree - Option () tunes a strategy.
-Xonly breaks ties; non-conflicting changes still merge.-X ours
bash
git merge -X ours feature # merge all, prefer ours on conflict
git merge -s ours feature # fake-merge: record, keep tree unchanged
git merge -X ignore-all-space feature
git merge -X patience feature # better diff for reordered blocks-s ours- 策略()用于选择算法:
-s(默认)、ort、recursive、resolve、octopus、ours。subtree - 选项()用于调整策略。
-X仅在冲突时优先选择我方内容;无冲突的变更仍会合并。-X ours
bash
git merge -X ours feature # 合并所有内容,冲突时优先选择我方版本
git merge -s ours feature # 伪合并:记录合并,但保持工作区不变
git merge -X ignore-all-space feature
git merge -X patience feature # 对重排代码块生成更优差异对比‑s oursInspecting
检查冲突
bash
git ls-files -u # stages 1=base, 2=ours, 3=theirs
git show :1:f > base; git show :2:f > ours; git show :3:f > theirs
git merge-file -p ours base theirs > resolved
git log --merge -p path # commits on either side touching file
git log --left-right HEAD...MERGE_HEADbash
git ls-files -u # 暂存区1=基础版本,2=我方版本,3=对方版本
git show :1:f > base; git show :2:f > ours; git show :3:f > theirs
git merge-file -p ours base theirs > resolved
git log --merge -p path # 查看双方分支中修改该文件的提交
git log --left-right HEAD...MERGE_HEADAbort
终止合并
bash
git merge --abort # before committing
git reset --merge # equivalentbash
git merge --abort # 提交前终止
git reset --merge # 等效命令Undoing a Completed Merge
撤销已完成的合并
Local only:
git reset --hard ORIG_HEADAlready pushed: (keep mainline, drop feature side).
git revert -m 1 <merge-sha>Gotcha: after , re-merging the same branch pulls only new commits — Git thinks the rest is already merged. Fix:
revert -m 1bash
git revert <revert-commit> # un-revert
git merge feature # clean re-merge仅本地场景:
git reset --hard ORIG_HEAD已推送至远程: (保留主线内容,舍弃特性分支的变更)。
git revert -m 1 <merge-sha>**注意事项:**执行后,重新合并同一分支时只会拉取新提交——Git认为其余内容已合并。解决方法:
revert -m 1bash
git revert <revert-commit> # 撤销之前的撤销操作
git merge feature # 干净地重新合并Pitfalls
常见陷阱
- No exists; do
-s theirson the other branch then merge back.-s ours - Octopus strategy refuses to auto-resolve conflicts.
- For recurring conflicts, enable rerere.
- 不存在策略;可在对方分支执行
-s theirs后再合并回来。-s ours - Octopus策略不会自动解决冲突。
- 若冲突反复出现,可启用rerere功能。