GitHub 合并陷阱排查
git-merge-hazards
GitHub 合并机制中的陷阱。当合并 PR、合并堆叠或串行的 PR 链、审查某个分支是否真正落地,或在 CI 失败(红灯)的情况下强行合并时使用。
- License 许可
- MIT
- 最近更新
- 2026-11-15
- Stars
- ★ 60
- Forks
- 6
01 前言 Front Matter
- name
- git-merge-hazards
- created
- 1787097600000
- modified
- 1794441600000
- reviewed
- 1787270400000
- description
- Traps in GitHub's merge machinery. Use when merging a PR, merging a stacked or serial PR chain, auditing whether a branch really landed, or merging over red CI.
- allowed-tools
- Read, Grep, Glob, Bash(gh pr *), Bash(gh issue *), Bash(gh api *), Bash(git cherry *), Bash(git merge-tree *), Bash(git rev-parse *), Bash(git rev-list *), Bash(git log *), Bash(git reflog *), Bash(git rebase *), Bash(git merge *), Bash(git checkout *), Bash(git worktree *), Bash(git diff *), Bash(git add *), Bash(git commit *), Bash(git -C *), Bash(git merge-base *), Bash(grep *), Bash(awk *), Bash(jq *), Bash(git push *), Bash(git fetch *), Bash(git cat-file *), Bash(sleep *), Bash(just *), Bash(bash *), TodoWrite
02 资源文件 13
- 参考文档references/closing-keyword-cases.md5.0 KB
- 参考文档references/delta-apply-stack.md8.7 KB
- 参考文档references/fork-upstream-base.md2.5 KB
- 参考文档references/merge-endpoint-failures.md5.2 KB
- 参考文档references/merge-shepherd.md7.3 KB
- 参考文档references/no-force-push-stack.md22.1 KB
- 参考文档references/serial-merge-chain.md7.6 KB
- 参考文档references/stacked-chain-push.md9.1 KB
- 参考文档references/sync-notes.md3.9 KB
- 脚本scripts/merge-chain.sh25.2 KB
- 脚本scripts/tests/test-merge-chain.sh41.3 KB
- 脚本scripts/tests/test-no-force-stack.sh114.7 KB
- 脚本scripts/tests/test-push-refspec.sh13.9 KB
03 SKILL.md 正文
Git Merge Hazards
When to Use This Skill
| Use this skill when... | Use something else when... |
|---|---|
| Merging a PR, or proving a branch landed | The PR's green CI ran against an older main → git-plugin:worktree-stale-base-merge |
| Deciding whether a red PR can merge (§4) | Deciding whether a red AI-review check is a real finding → github-actions-plugin:ai-review-max-turns |
| Merging an ad-hoc stacked chain (§2, §3) | Release-please PRs are piling up or conflicting → git-plugin:release-please-pr-workflow |
The numbered sections mirror the ~/.claude/rules/pr-merge-hazards.md rule;
differences: references/sync-notes.md.
Six traps in GitHub's merge machinery, one law: "PR merged" says nothing about
content — and a red check is not proof of failure. Sibling: git-plugin:git-local-hazards (local git).
1. --merged misses squash-merged branches
A squash-merge collapses a branch into one fresh-SHA commit on main, so the
branch's own commits are never ancestors — git branch --merged (and any
ancestry check) reports it unmerged. "Files identical to main" also fails
once main drifts the same files.
- Check, in order of authority:
gh pr list --state all --head <branch> --json state→ a MERGED PR is authoritative. Reach for this first; the git-side checks below are all one-way.git cherry main <branch>→ marks a commit-when a patch-equivalent commit is already upstream,+when it is not. Survives squash and cherry-pick, and does not care thatmaindrifted.git merge-tree --write-tree main <branch>equalsgit rev-parse main^{tree}→ contained. A match proves containment; a non-match proves nothing.
- Not immune to drift (corrected 2026-07): once
mainmoves on over the same files, merging an already-merged branch back would re-introduce its older versions, so the trees differ and merge-tree reports not contained for work that fully landed. Observed reporting three merged branches as unmerged. Same trap as "files identical to main". Use the PR state orgit cherryto decide; keep merge-tree only as a positive-containment shortcut. - Fix:
just -g branch-audit(inprivate_dot_config/just/git.just) prints MERGED vs REVIEW + a paste-ready delete. Its REVIEW bucket measured ~90% false positives (#2268), so a REVIEW line is a question to settle withgh pr list --state all --head <branch>, not a verdict. - A non-match is "review", not proof of unmerged — don't force the count to zero.
2. Merging a stacked base auto-CLOSES the child PR
When PR B is based on PR A's branch, merging A and deleting its branch auto-closes B (GitHub does not retarget it), and a closed PR whose base branch is gone cannot be reopened.
- Check the base first (skill-only):
gh pr view <base> --json state,mergeStateStatus,mergeablebefore retargeting. Proceed only whenstateisOPENandmergeableisMERGEABLE; themergeStateStatusvalues and theUNKNOWNwait are in references/no-force-push-stack.md. - Fix — order matters: retarget the child first, while the base PR is open:
gh pr edit <child> --base maingh pr merge <base> --<method> --delete-branch(method:pr-merge-method.py)git rebase --onto origin/main <old-base-tip> <child-branch>(drops the base's commits) +git push --force-with-lease- merge the child.
- No-force alternative (skill-only, preferred when the child builds only on its base): merge
maininto the child with no force-push, after a classification that decides whether-X oursis lossless. Recipe: references/no-force-push-stack.md. The rebase path in the Fix steps is the fallback. - Delta-apply (skill-only) when the child forked from an older base head, or an independent PR shares its files: references/delta-apply-stack.md.
- Fork PR on an open upstream PR's head (skill-only): merging pushes into that PR too: references/fork-upstream-base.md.
- If already auto-closed: the head branch survives — rebase as above,
gh pr createfresh, comment "Superseded by #new" on the closed one. - Nothing tells you this happened. The auto-close is silent: no failed
check, no notification, and the PR list just looks one shorter. claude-plugins
#2049 sat stranded for a day; a sweep then found 26 dead branches, two carrying
work that had never had a PR opened at all (so no event ever fired for
them either). A scheduled sweep is the only thing that finds this class —
an event handler on
pull_request: closedis too late by construction (the base ref is already deleted, so the reopen window is gone) and is blind to never-PR'd branches.claude-plugins scripts/check-stranded-work.shis the encoded audit; it takes--repo, so one run sweeps the portfolio. - Telling an accident from a decision: a closed-unmerged PR whose base ref 404s was auto-closed; one whose base ref is still alive was closed by a human (duplicate/superseded). That single check is the discriminator — 11 of those 26 branches were deliberate closes and must not be resurrected.
3. Stacked-chain merges: push by SHA, never HEAD: — and expect auto-close races
Read references/stacked-chain-push.md before any force-push or merge in a stacked-PR chain. It carries the push-by-SHA refspec form (braced variable, full refs/heads/ refname, the zsh word-modifier table), the empty-diff auto-close, the mergeability and CI-registration races, the pre-push check, and auto-close recovery.
4. A red PR may still be mergeable — UNSTABLE is not BLOCKED
mergeStateStatus separates required failing checks (BLOCKED — merge
refused) from merely-present ones (UNSTABLE — plain gh pr merge works), so
--admin on an UNSTABLE PR takes a privilege you didn't need. Read it first.
Merging over red needs two checks: --json files (config/docs can't break
a compile) and the same check already failing on main. Either alone is a
guess — and a stale-green main lies, so check createdAt (2026-07: a "green"
run was 21 days old; main hadn't compiled for three weeks).
A 500 from the merge call may be GitHub, not the PR: run the stale-SHA control in
references/merge-endpoint-failures.md before changing the PR.
Several PRs, ruleset requiring branches up to date: run scripts/merge-chain.sh
(§6); a plain merge leaves the rest BEHIND.
5. A negated closing keyword still closes the issue
GitHub matches close|fixes|resolves|… + an issue reference without parsing the
sentence around it, so the natural way to disclaim closure closes the issue.
Markdown emphasis between verb and number doesn't break the match either:
Does **not** close #162 → GitHub reads `close #162`, and closes it
- Check:
gh issue view <n> --json closedByPullRequestsReferencesnames the closer; aclosedAtone second after a merge is automation, not a decision. - Fix: never let a closing verb precede an issue number you don't mean to
close —
Related: #162,Unblocks #162, or#162 stays open — it needs …. - Recovery:
gh issue reopenworks (nothing was deleted, unlike #2), then comment so the next reader doesn't re-derive it.
The trap is wider than negation: plain past tense, a markdown-linked cross-repo reference, and a keyword you are only quoting all match too. Before merging a PR whose body mentions issue numbers, read references/closing-keyword-cases.md for those cases, the audit-with-a-control procedure, and the incidents.
6. A serial merge chain merges commits nobody verified
gh pr update-branch and gh pr edit --base rerun pull_request workflows,
so a branch-writing bot's post-review commits land unread. Before each merge,
refuse post-review commits but GitHub's update-branch merge (2+ parents,
committer noreply@github.com); your own main merge is an unreviewed
resolution. Run git rev-parse <ref> as its own call; paste the
full 40-character SHA into --match-head-commit
(the merge guard refuses $(…)). A PR changing that bot goes last. See references/serial-merge-chain.md.
Merge shepherd
For an ordered PR list run scripts/merge-chain.sh. An agent's CONFLICT/CI_RED
fix is unreviewed: pass it through
workflow-orchestration-plugin:workflow-implement-verify's repair-round:
references/merge-shepherd.md.
Agentic Optimizations
| Context | Command |
|---|---|
| Did the branch land? (§1, authoritative) | gh pr list --state all --head <branch> --json number,state,mergedAt |
| Can a red PR merge? (§4) | gh pr view <n> --json mergeStateStatus,mergeable — UNSTABLE merges, BLOCKED refuses |
| Before a force-push (§3) | git log --oneline origin/main..<sha> — exactly the child's commits |
04 同仓库更多 Skills
session-workflow-capture
Workflow-tool scripts and workflow friction from this session, filed as issues here or in the plugins repo. Use when a session ran Workflow scripts.
kitty-show-image
Show the user an image with a note on what to judge, via a file send or a labelled kitty tab. Use when the user must see an image, or icat fails with no /dev/tty.
kitty-session-persistence
Snapshot and restore kitty terminal sessions on macOS. Use when surviving WindowServer hangs without losing tabs, configuring snapshots, or restoring after a force-reboot.
kitty-remote-control
Safe kitty remote control from an agent — read panes with kitty @ ls/get-text, notify, and never mutate the live terminal unasked. Use when running inside kitty.
docs-reference-check
Dangling doc-reference gate: flag markdown naming repo paths or relative links that no longer exist. Use when docs cite moved/deleted files, or wiring a doc-drift lint.
YouTube 视频上传与编辑
youtube-upload
通过 Data API v3 将视频上传到 YouTube,并在之后编辑其标题、描述、标签或缩略图。适用于发布 YouTube 视频或修改其标题。