/agent-traffic-control:pr-hijack-via-stale-worktree-branch-reffrom agent-traffic-control
Diagnose and recover from accidentally overwriting another session's open PR when `git push -u origin <branch>` in a long-lived worktree silently replaces the remote branch ref. Use when ANY of the following appears: (1) you ran `git checkout -b <branch> origin/main` and `git push -u` and git reported `* [new branch]` even though the branch already existed upstream, (2) `gh pr create` then errors with `a pull request for branch "<branch>" into branch "main" already exists` pointing to a PR you didn't author, (3) `gh pr view N --json commits` on that PR shows YOUR commit instead of the one the title/body describes, (4) you discover your worktree's session-start git status banner was stale (showed branch X but actual branch was Y). Pattern surfaces when a prior session in the SAME worktree opened a PR on a generically-named feature branch, then reset the worktree's local refs without cleaning the remote. Recovery requires `git push --force-with-lease` — a destructive op requiring explicit user authorization. Sister to `git-recover-lost-branch` (mechanics of recovering an orphaned commit via reflog) and `gh-pr-merge-worktree-checkout-trap` (different worktree-vs-gh gotcha).
Trending skills
7-day visitors to each skill page
Ships in agent-traffic-control. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description, though model invocation is off: only you can start it.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
In a long-lived git worktree (especially .claude/worktrees/<name>/ patterns
created by a parallel-session harness), a same-named local branch can be
created via git checkout -b X origin/main even when branch X already
exists upstream with an open PR. Pushing that local branch with
git push -u origin X succeeds — git reports * [new branch] — and
silently replaces the remote ref. The result:
gh pr create fails with a pull request for branch "X" into "main" already exists and surfaces PR #N — which is now an artefact of two
unrelated commitsThe hijack is silent on both sides: git's push output doesn't warn, gh doesn't either, and the PR's web view shows a perfectly plausible (but wrong) state.
Surfaces when ALL of:
.claude/worktrees/<name>/)
that has been used by a previous sessiongit status -sb once you actually run it (stale banner)feat/drivers-headline-and-stripe-removal,
feat/refactor-foo, fix/bar-bug)git reset --hard, branch deletion, or worktree-recreation) WITHOUT a
corresponding git push --force to clean up the remoteExact failure-signature triad:
# Symptom 1 — push reports new-branch despite upstream existing
$ git push -u origin feat/<name>
* [new branch] feat/<name> -> feat/<name>
# Symptom 2 — gh pr create rejects with already-exists
$ gh pr create ...
a pull request for branch "feat/<name>" into branch "main" already exists:
https://github.com/<org>/<repo>/pull/N
# Symptom 3 — that PR's commits don't match its title/body
$ gh pr view N --json commits
{ "commits": [{ "oid": "<YOUR-SHA>", "messageHeadline": "<YOUR WORK>" }] }
If the gh-pr-view output shows YOUR work and the PR title describes something entirely different — confirmed hijack.
Stop before doing anything else. Recovery requires a destructive force-push to a remote branch you don't own. Ask the user for explicit authorization first.
git reflog --date=iso | grep -E "<branch-name>|<keyword-from-pr-title>" | head -10
The reflog entry will look like:
<orig-sha> HEAD@{<timestamp>}: commit: <ORIGINAL PR TITLE>
<base-sha> HEAD@{<timestamp>}: checkout: moving from <old> to <branch>
Verify the orphaned commit is still reachable:
git cat-file -t <orig-sha> # should print "commit"
git log --oneline <orig-sha> -3
If the reflog is empty or doesn't contain the SHA, you may need to:
git fsck --unreachable (orphan-walk)gh pr view N --json headRefOidCritical ordering: create the rescue branch BEFORE resetting the current branch. Otherwise you'll have nothing to push to a new branch.
git branch <new-branch-name> <YOUR-SHA> # e.g., feat/<descriptive-new-name>
git branch -v | grep <descriptive> # verify both branches exist
git reset --hard <orig-sha>
git log --oneline -2 # confirm HEAD is now at the original commit
git push --force-with-lease=<branch>:<YOUR-SHA> origin <branch>
--force-with-lease=<branch>:<YOUR-SHA> says "only push if the remote
ref is currently exactly YOUR-SHA" — which it is, since you just pushed
it. This guards against clobbering anything a third party may have pushed
since.
After this, gh pr view N --json commits should show the original SHA
and the PR is fully restored.
git checkout <new-branch-name>
git push -u origin <new-branch-name>
gh pr create --title "..." --body "..."
Cross-link to PR #N in the new PR's "branch history" or "notes for reviewer" section. Mention the two PRs touch the same page/area but different code paths. This avoids the reviewer wondering why two PRs on sister branches exist.
After recovery, verify all three sides:
# Local reflects truth
git log --oneline <original-branch> # → original SHA on top
git log --oneline <new-branch> # → your SHA on top
# Remote reflects truth
gh pr view N --json title,commits | python3 -m json.tool
# title and commits should describe ORIGINAL work
gh pr view <NEW-N> --json title,commits
# title and commits should describe YOUR work
# CI on both PRs should re-run automatically after the force-push
gh pr checks N
gh pr checks <NEW-N>
The trigger is stale local refs in a long-lived worktree. Two habits prevent it:
Verify branch state at session start. When entering a worktree,
git status -sb && git log --oneline -3 && git ls-remote origin <branch-you-plan-to-create> | head -2. If ls-remote shows the
branch exists upstream and your local reflog has no record of it,
pick a different name.
Prefix branch names with session/date. Generic names like
feat/refactor-foo collide across sessions. feat/<YYYYMMDD>-foo,
feat/<session-id>-foo, or worktree-scoped names eliminate the
collision class entirely. The repos that have this convention —
session- or date-prefixed synthetic IDs, applied consistently —
don't see this bug.
If your worktree harness shows a session-start git status banner, don't
trust it. The banner is captured at worktree creation and can be stale
after subsequent operations. Always run git status -sb first.
check-a-stale-branch-is-not-undoing-newer-main-work
(that's about merging a stale PR clobbering main's progress). The trigger
here is the OPPOSITE direction: your local clobbering the remote on push.gh-pr-merge-worktree-checkout-trap (failed
merge due to worktree-locks-main). Here the merge isn't involved; the
collision happens at push time before any merge.gh pr view N --json headRefOid BEFORE resetting to capture the full upstream tip.git push -u origin <branch> reporting * [new branch] is the
EARLIEST detectable signal. If you see that message and the branch name
is generic, immediately gh pr list --head <branch> to check for
pre-existing PRs before continuing.Real session (2026-05-12, the-project-repo repo, Opus 4.7):
review-driver was created from a session ~12:33Z that
committed e4796f17 Fix /drivers chevron: filter NULL raw_value rows
to feat/drivers-headline-and-stripe-removal, pushed, opened PR #761.origin/main but left
the remote branch + PR alone./impeccable critique driver page, decided to fix headline + side-stripes, did git checkout -b feat/drivers-headline-and-stripe-removal origin/main — collided
with the upstream ref but git locally treated it as new.5107c97f and pushed; got * [new branch].gh pr create returned "a pull request for branch X already exists:
#761". gh pr view 761 showed PR #761's title was still the chevron
fix, but the only commit listed was 5107c97f (the hijacker).git branch feat/drivers-headline-take 5107c97f →
git reset --hard e4796f17 → git push --force-with-lease=feat/ drivers-headline-and-stripe-removal:5107c97f origin feat/ drivers-headline-and-stripe-removal → git checkout feat/drivers- headline-take → git push -u origin feat/drivers-headline-take →
gh pr create → PR #762. Total recovery time: ~3 minutes once the
hijack was diagnosed.git push --force-with-lease:
https://git-scm.com/docs/git-push#Documentation/git-push.txt---force-with-leaseltrefnamegtgh-pr-merge-worktree-checkout-trap (sister skill, different
worktree-vs-gh failure mode)What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
npx claudepluginhub wan-huiyan/agent-traffic-control --plugin agent-traffic-control118 skills in this plugin
/agent-refusal-with-evidence-beats-literal-complianceA dispatched agent that does exactly what you asked can ship a defect you would
have stopped yourself, because it can see something at the worksite that you
could not see when you wrote the brief. Use when: (1) you are writing a brief
that prescribes a specific fix, threshold, wording or value rather than the
outcome you want; (2) an agent reports that it did not do what you asked and
you are deciding whether that is insubordination or judgement; (3) an agent
asserts "this is fine" or "not needed" with no measurement behind it; (4) you
are reviewing overnight output and want to know which decisions were checked
and which were assumed. Two halves, and both are needed: give the agent
explicit permission to satisfy the INTENT of an instruction while refusing its
letter, and REQUIRE the evidence with the refusal — what it built or measured,
what that showed, what it did instead. Evidence is the whole difference between
a refusal you can bank and one you have to re-litigate. Four agents correctly
declined an instruction in a single run; one built the suggested fix, measured
that it turned a silent safe refusal into a permanently red pipeline, and
reverted it unpushed.
/auto-merge-rearms-while-agent-live-kill-then-disarm-verifyDisarming auto-merge on a PR whose agent is still running does not hold it —
the agent re-arms within minutes and the PR merges, because arming is a step in
the agent's brief and disarming is not an instruction to the agent at all. Use
when: (1) you want to STOP a pull request from merging that an agent opened and
armed, for review, for a rethink, or because it is landing into a queue you are
trying to drain; (2) a PR you disarmed merged anyway; (3) you are telling agents
which PRs are on hold and want the hold to be enforced rather than announced;
(4) you are about to run `gh pr merge --disable-auto` and have not decided what
to do about the agent. The order is KILL THE AGENT FIRST, THEN DISARM, THEN
VERIFY the disarm by reading `autoMergeRequest` back — a disarm you did not read
back is not a hold. The stronger form needs no ordering at all: convert the PR
to a draft with `gh pr ready --undo`. A draft cannot merge whatever any agent
does to it, so the hold survives an agent you missed, an agent that restarts,
and an agent you did not know about.
/batch-member-cites-a-path-only-a-non-member-addsYou wrote something correct that the default reader cannot reach: a batch member citing a
path only a NON-member adds, or a rule, ruling or ledger sitting on an unlanded branch.
/batch-merge-subject-is-not-evidence-a-member-landedYou are about to close, delete the branch of, or mark as landed a pull
request because a batch, roll-up or merge-train commit on the target branch
names it in the subject line, and because the member's own commits all
predate that merge by wall clock. Neither of those is evidence. A batch
branch is assembled at some moment BEFORE it merges, so anything the member
pushed after assembly is outside it, and the subject records INTENT at
assembly time rather than CONTENT at merge time. Use before closing a
member, when an ancestry check against a squashed batch says no, or when a
member's branch still shows commits ahead of the target. Compare file
content against the target branch instead, expecting exactly one legitimate
difference: the shared aggregate file every member appends to.
/check-a-stale-branch-is-not-undoing-newer-main-workA branch built on an out-of-date main is about to merge, or has merged, and may undo work
that landed since. Use when a green, conflict-free PR deletes files you never touched,
newer values or a whole redesign vanish after a sibling PR merged, rows of a shared ledger
(tracker, registry, CHANGELOG) disappear or revert, or when `git diff main...HEAD` or a
reviewer says your small PR touches dozens of files you did not change.
/ci-leg-skipping-moves-minutes-it-does-not-remove-themYou are about to propose, price or approve a CI cost saving that works by
making a job or matrix leg SKIP on pull requests — a path filter, a safe
list, a changed-files gate, an exemption list. Check first whether the
pipeline runs a complement or backstop on merge, on push, or nightly:
many do, and then the legs a pull request skipped are exactly the legs some
later run executes, so a branch that goes green once and merges bills the
same total either way and the wall clock does not move at all. Use when
pricing a filter change, when a percentage saving is quoted with no basis,
or when choosing between filtering legs and batching pull requests. The
question that separates money from bookkeeping is whether the change
removes RUNS or merely moves LEGS.