git-advanced-merging

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Advanced 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 conflict
Markers become
<<<ours / ||| base / === / theirs >>>
— resolution is far easier.
默认会隐藏共同祖先。如需显示:
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 path
During 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

  • Strategy (
    -s
    ) picks the algorithm:
    ort
    (default),
    recursive
    ,
    resolve
    ,
    octopus
    ,
    ours
    ,
    subtree
    .
  • Option (
    -X
    ) tunes a strategy.
    -X ours
    only breaks ties; non-conflicting changes still merge.
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
says "we abandon their work but don't want future merges replaying it."
  • 策略
    -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 ours
的作用是“我们舍弃对方的工作,但不希望未来合并时重新应用这些内容”。

Inspecting

检查冲突

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_HEAD
bash
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_HEAD

Abort

终止合并

bash
git merge --abort                   # before committing
git reset --merge                   # equivalent
bash
git merge --abort                   # 提交前终止
git reset --merge                   # 等效命令

Undoing a Completed Merge

撤销已完成的合并

Local only:
git reset --hard ORIG_HEAD
Already pushed:
git revert -m 1 <merge-sha>
(keep mainline, drop feature side).
Gotcha: after
revert -m 1
, re-merging the same branch pulls only new commits — Git thinks the rest is already merged. Fix:
bash
git revert <revert-commit>          # un-revert
git merge feature                   # clean re-merge
仅本地场景:
git reset --hard ORIG_HEAD
已推送至远程:
git revert -m 1 <merge-sha>
(保留主线内容,舍弃特性分支的变更)。
**注意事项:**执行
revert -m 1
后,重新合并同一分支时只会拉取新提交——Git认为其余内容已合并。解决方法:
bash
git revert <revert-commit>          # 撤销之前的撤销操作
git merge feature                   # 干净地重新合并

Pitfalls

常见陷阱

  • No
    -s theirs
    exists; do
    -s ours
    on the other branch then merge back.
  • Octopus strategy refuses to auto-resolve conflicts.
  • For recurring conflicts, enable rerere.
  • 不存在
    -s theirs
    策略;可在对方分支执行
    -s ours
    后再合并回来。
  • Octopus策略不会自动解决冲突。
  • 若冲突反复出现,可启用rerere功能。