Skip to content
Back to skills

Git Merge Hazards

ASecurity

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.

  • 60 stars
  • 0 votes
  • 0 copies
  • 10 views
  • Added September 3, 2026
toolspythongobashgitapi

Works with

  • api

Security analysis

A100/100

Pro scans all 2 files and shows the line behind each finding

Scanned November 26, 2026

npx -y skills add laurigates/claude-plugins --skill git-merge-hazards --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Git Merge Hazards?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Git Merge Hazards
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/laurigates-git-merge-hazards/badge)](https://www.skillsdirectory.com/skills/laurigates-git-merge-hazards)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: git-merge-hazards
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
created: 2026-08-19
modified: 2026-11-25
reviewed: 2026-08-21
---

# 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](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 that `main` drifted.
  - `git merge-tree --write-tree main <branch>` equals `git rev-parse main^{tree}`
    → contained. **A match proves containment; a non-match proves nothing.**
- **Not immune to drift** (corrected 2026-07): once `main` moves 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 or `git cherry` to decide;
  keep merge-tree only as a positive-containment shortcut.
- **Fix**: `just -g branch-audit`
  (in `private_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 with `gh 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,mergeable` before retargeting. Proceed only when `state` is `OPEN` and `mergeable` is `MERGEABLE`; the `mergeStateStatus` values and the `UNKNOWN` wait are in [references/no-force-push-stack.md](references/no-force-push-stack.md#check-the-base-first).
- **Fix — order matters**: retarget the child **first**, while the base PR is open:
  1. `gh pr edit <child> --base main`
  2. `gh pr merge <base> --<method> --delete-branch` (method: `pr-merge-method.py`)
  3. `git rebase --onto origin/main <old-base-tip> <child-branch>` (drops the
     base's commits) + `git push --force-with-lease`
  4. merge the child.
- **No-force alternative (skill-only, preferred when the child builds only on its base)**: merge `main` into the child with no force-push, after a classification that decides whether `-X ours` is lossless. Recipe: [references/no-force-push-stack.md](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](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](references/fork-upstream-base.md).
- **If already auto-closed**: the head branch survives — rebase as above,
  `gh pr create` fresh, comment "Superseded by #new" on the closed one.
- **Nothing tells you this happened**, and only a scheduled sweep finds it; a dead base ref tells an auto-close from a human close: [references/stranded-child-audit.md](references/stranded-child-audit.md).

## 3. Stacked-chain merges: push by SHA, never `HEAD:` — and expect auto-close races

Read [references/stacked-chain-push.md](references/stacked-chain-push.md) before any force-push or merge in a stacked-PR chain. It carries the push-by-SHA refspec form, 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 tests 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](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 closedByPullRequestsReferences` names the
  closer; a `closedAt` one 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 reopen` works (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](references/closing-keyword-cases.md) for those cases and the audit procedure.

## 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](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](references/merge-shepherd.md). A skipped release-please PR merges via `git-plugin:release-please-pr-workflow`.

## Rebase or squash

`pr-merge-method.py` decides, by the table in [merge-strategies.md](../github-pr-title/merge-strategies.md). Two or more independent conventional commits rebase; everything else squashes, including single commits, merge-synced branches, fixups and bot PRs. Commits that all share one `type(scope)` count as a single concern and squash (`single_concern`), however many there are.

## 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 |
| Rebase or squash? (§2, §6) | `python3 "${CLAUDE_PLUGIN_ROOT}/scripts/pr-merge-method.py" --pr <n>` |
| Before a force-push (§3) | `git log --oneline origin/main..<sha>` — exactly the child's commits |

Files in this skill

  • SKILL.md14.1 KB
  • scripts/tests/test-push-refspec.sh11.9 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in tools →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.