Merges reviewed PRs via squash or rebase, then cleans up branches and worktrees. Handles integration branches, pre-merge rebase checks, and worktree detection.
Ships in caliper. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Merge (squash or rebase) and clean up branches and worktrees.
Prerequisite: A PR that has been reviewed (via /pr-review or manually).
Detect if CWD is inside a worktree — it is when the two printed lines differ (the isolation guard refuses $(git …) in a test):
git rev-parse --path-format=absolute --git-dir --git-common-dir
If inside a worktree, note IN_WORKTREE=true and capture paths for cleanup:
MAIN_REPO=$(caliper-main-root)
WORKTREE_PATH=$(pwd)
CWD_BRANCH=$(git rev-parse --abbrev-ref HEAD)
Stay in the worktree — gh pr merge is a GitHub API call that works from any directory.
Identify the PR from argument, current branch (gh pr view), or gh pr list --author @me --state open. If multiple candidates and you're not on a branch with an associated PR, ask the user to pick. Store PR number, branch name, and URL.
Detect environment:
DEFAULT_BRANCH from refs/remotes/origin/HEAD (fallback: main/master)BASE_BRANCH — the PR's base, from gh pr view $PR_NUMBER --json baseRefName --jq .baseRefName (fallback: DEFAULT_BRANCH)IS_INTEGRATION — true when $BRANCH_NAME matches integrate/*IS_INTEGRATION_CWD — true when $CWD_BRANCH matches integrate/* (CWD is an integration worktree, regardless of which PR is being merged)If branch protection requires human approval and the PR lacks it, tell the user and stop with the PR URL.
Pre-merge rebase check: Verify the PR branch is up-to-date with <BASE_BRANCH>. Checking the default branch instead would read a phase PR as behind once main moves, and rebasing it onto main would pull unrelated commits into the phase PR:
git fetch origin
git merge-base --is-ancestor origin/<BASE_BRANCH> origin/<BRANCH_NAME>
Compare the PR's pushed head, not HEAD: pr-merge can run from a checkout of another branch (the integration worktree for a phase PR, or the default branch with no worktree), where HEAD isn't the PR branch.
Use bare git fetch origin (no branch arg) so refs/remotes/origin/<BASE_BRANCH> and origin/<BRANCH_NAME> actually advance. git fetch origin <BASE_BRANCH> only updates FETCH_HEAD — the is-ancestor check then compares against a stale ref and reports up-to-date when the branch is actually behind.
Exit 1 means behind. Any other non-zero exit means a ref didn't resolve (e.g. a fork PR has no origin/<BRANCH_NAME>): report it and stop rather than guess. If behind, work in the PR branch's own worktree: the CWD when CWD_BRANCH is $BRANCH_NAME, else the path Step 3's phase-worktree lookup prints for refs/heads/<BRANCH_NAME>. Rebase there only if its tree is clean and git rev-parse HEAD equals git rev-parse origin/<BRANCH_NAME>: a local branch that is behind would drop pushed commits, and one that is ahead would ship unreviewed work. Otherwise report the mismatch and stop. Rebase onto origin/<BASE_BRANCH>, resolve conflicts, run tests, and push with the lease pinned to the checked sha, git push origin HEAD:<BRANCH_NAME> --force-with-lease=<BRANCH_NAME>:<checked sha>. A bare lease passes right after a fetch, so it can't catch a concurrent push. Comment on PR with conflict resolution details. Complex conflicts → stop and ask user. If no worktree has the branch checked out, report that the PR is behind <BASE_BRANCH> and stop, rather than rebasing a checkout that belongs to another branch.
Merge method ($METHOD = squash or rebase):
IS_INTEGRATION=true): rebase — auto-detected, no flag neededintegrate/*): squash — auto-detected, no flag needed--rebase flag overrides for any non-auto-detected branchcaliper-settings get merge_strategy (squash or rebase)Multi-phase plans produce one squash commit per phase on the integration branch; rebase preserves that per-phase history on main. Single-phase plans use squash (one phase = one commit).
Enable auto-merge (preferred). Hand the CI gate to GitHub instead of polling gh pr checks yourself. Check whether the repo allows it:
ALLOW_AUTO_MERGE=$(gh api "repos/{owner}/{repo}" --jq .allow_auto_merge 2>/dev/null)
If true, enable auto-merge:
gh pr merge $PR_NUMBER --auto --$METHOD
This is non-blocking — it returns once auto-merge is enabled, not once the PR merges. GitHub performs the merge whenever required checks pass (PR already mergeable → merges within seconds; checks pending → deferred until green). A non-zero exit means auto-merge couldn't attach (repo disallows it, or the PR is already in a clean immediately-mergeable state GitHub won't queue) — fall back to the direct merge below.
Fallback — direct merge. When allow_auto_merge is false or --auto exits non-zero, gate on the checks first (gh pr merge errors while required checks pend): caliper-wait-pr $PR_NUMBER --timeout-min <merge_wait_minutes> — --timeout-min 0 (one poll) under --no-wait. On exit 0 GREEN or 4 NO_CHECKS, gh pr merge $PR_NUMBER --$METHOD (returns with the PR MERGED); on 2, 5 or 6, the matching row below; otherwise report the reason line and stop.
Wait for the merge to land. Unless --no-wait was passed — a hand-rolled state poll waits out the timeout on a conflict or failed check that blocks auto-merge:
caliper-wait-pr $PR_NUMBER --merged --timeout-min <merge_wait_minutes>
Minutes from caliper-settings get merge_wait_minutes (default 10) — not review_wait_minutes, which orchestrate overloads to 0 ("merge directly"). Run either wait with run_in_background: true: it's minutes of idle polling that would pin a foreground call (capped at 10 min), and the harness re-invokes you when it exits — don't poll its output. Its last line opens with the state named in the table. The --timeout-min 0 single poll can stay in the foreground.
| Exit | Then |
|---|---|
5 MERGED | Step 3 (direct merge returns this at once) |
6 CLOSED | Stop and report; no cleanup |
2 CONFLICT | Base moved: redo the pre-merge rebase, push, rerun Step 2 |
1 FAILED | Report the failed checks and PR URL; skip Step 3 (a non-required failure may still auto-merge) |
3 TIMEOUT · 4 NO_CHECKS | Auto-merge hasn't fired. Report the reason line (what's pending, or that no check registered — a required review or untriggered required check is blocking) and the PR URL; skip Step 3 |
Skipping Step 3 is safe: a later /pr-merge sees MERGED and finishes cleanup (Step 3's per-branch gh-state gate makes re-runs safe).
--no-wait enables auto-merge and exits after reporting, skipping Step 3.
Never use --delete-branch — branch cleanup is handled in Step 3.
Read the repo's auto-delete-on-merge setting (AUTO_DELETE_REMOTE, used below) and refresh remote-tracking refs so the containment guard sees the just-merged commit (bare git fetch — see Step 2 note):
gh api "repos/{owner}/{repo}" --jq .delete_branch_on_merge
git fetch origin
Branch deletion is gh-verified, local first. <B> is the call site's branch ($BRANCH_NAME, phase-a, …); <PR> is the PR to verify against — pass $PR_NUMBER (the just-merged PR from Step 1) when <B> is $BRANCH_NAME, since resolving by branch name returns the most recent PR for that name, which for a reused name like phase-a may be a stale historical one. Omit it to resolve by branch for the phase-X branches in integration cleanup: $PR_NUMBER there is the integration PR, which vouches for no phase tip, so every phase branch would SKIP:
delete-merged-branch <B> <PR>
It deletes the local branch only when GitHub reports the PR merged and the local tip is provably what landed — exactly headRefOid, an ancestor of the merge commit, or tree-identical to it (squash/rebase) with no commits on top of headRefOid — and no worktree has it checked out. Every leg is fail-closed and the delete is a compare-and-swap, so commits added after the merge are never destroyed silently. Exit 0 prints DELETED <B> <head> and, when <head> is known, the leased remote delete on a second line. Exit 2 prints a GONE/SKIP line saying why it kept the branch — a report, not a failure: note it for the Step 4 Summary and carry on.
Remote branch — only when the helper printed a push line, and only when AUTO_DELETE_REMOTE isn't true (else GitHub already deleted it on merge): run that git push --force-with-lease=refs/heads/<B>:<head> origin --delete refs/heads/<B> line verbatim rather than filling the template by hand — a lease protects only the ref it names, so a line whose two <B>s differ deletes unchecked, and the helper shell-quotes a branch name holding shell syntax.
The lease makes it a compare-and-delete: the push is rejected unless origin's tip is still the merged head, so a branch another writer advanced after the merge survives. remote ref does not exist means it's already gone; any rejection (stale info = advanced past the merged head, or protected) → report SKIP remote <B>. A DELETED line without <head> (and so no push line) means leave the remote branch and report it. This stays its own visible call so a safety hook guarding remote deletes can see and gate it; the local cleanup above doesn't depend on it.
Worktree removal uses bare git worktree remove <wt> (no --force). Before each one, run clear-worktree-scratch <wt>: it syncs agent memory back to main, then deletes caliper's own untracked scratch (what discard_changes used to discard) so only user content can block the remove. This stop-on-failure rule applies to every git worktree remove call in this section: if clear-worktree-scratch or the removal exits non-zero (a failed clear means memory may be unsynced, and an ignored .claude/ wouldn't stop the remove from deleting it), the worktree holds content the user may want — stop the cleanup chain, report the path, and let the user decide rather than force-removing it. Phase worktrees are located by branch, not a built path (nested in the integration worktree, siblings in older plans; earlier runs may have removed some):
git worktree list --porcelain | awk '/^worktree /{w=substr($0,10)} $0=="branch refs/heads/phase-X"{print w}'
No output: already gone. Otherwise remove the printed path.
Leave and remove the current worktree (used below) — first match wins:
…/<feature>/.claude/worktrees/phase-X): skip ExitWorktree (the session belongs to the integration worktree, which orchestrate keeps using): cd there as its own Bash call, confirm with pwd in the next that the cwd left $WORKTREE_PATH (if not, stop and report), then remove it, and skip the git pull --rebase below — orchestrate fast-forwards integrate in 7d.ExitWorktree with action: "remove", discard_changes: true (the PR merged, so local commits are safe to discard).
path): ExitWorktree with action: "keep" lifts isolation and returns to the main checkout; then remove $WORKTREE_PATH.cd <MAIN_REPO> && pwd as its own Bash call (no git in it), with literal paths. A successful cd to a directory inside the session's working directories carries over to the next call; with no worktree session the main checkout is one. Continue only if that call succeeded and the next call's cwd is <MAIN_REPO>: then run git worktree remove <WORKTREE_PATH>. If the cd failed or didn't stick, stop the cleanup chain and report both paths. Removing the directory the session is sitting in leaves every later call in a deleted cwd, and safety hooks then refuse all tools until a restart. Don't swap in git -C <MAIN_REPO> worktree remove … to avoid the cd: that deletes the worktree while the session is still inside it. A separate cd call also satisfies setups that forbid cd … && git … in one command.Integration branch (IS_INTEGRATION=true):
phase-X) — nested ones sit inside the integration worktree, so they go firstIN_WORKTREE: leave and remove the current worktreephase-X from plan.json, apply the pattern above without <PR>$BRANCH_NAME (gh-verified)git worktree prune && git pull --rebase && git remote prune originStandard worktree (IN_WORKTREE=true):
IS_INTEGRATION_CWD=true: pr-merge is running from the integration worktree for a phase PR (a manual run — orchestrate uses the phase worktree) — do NOT remove the integration worktree. Just delete $BRANCH_NAME (gh-verified) and prune remotes (git remote prune origin). While the phase worktree still exists the delete reports SKIP … still checked out — expected; the integration branch's own cleanup deletes the phase branch after removing its worktree. The orchestrator handles the rest in Phase Wrap-Up 7d/7e.IS_INTEGRATION_CWD=false (normal case, CWD branch matches PR branch):
$BRANCH_NAME (gh-verified)git worktree prune && git pull --rebase && git remote prune originNo worktree: git checkout <DEFAULT_BRANCH> && git pull --rebase && git remote prune origin, then delete $BRANCH_NAME (gh-verified).
Report: PR number/URL, merge status, cleanup status.
| Arg | Effect |
|---|---|
<PR number> | Target specific PR (/pr-merge 42) |
| (none) | Detect from current branch |
--rebase | Use rebase merge instead of squash (for multi-phase final PRs) |
--no-wait | Enable auto-merge and exit without waiting for the merge or cleaning up (cleanup runs on a later /pr-merge once GitHub reports MERGED) |
| Mistake | Why |
|---|---|
Skipping ExitWorktree when it's available | Inside an EnterWorktree session, cd out of the worktree doesn't carry over to later Bash calls — only ExitWorktree resets CWD at the session level. Always try ExitWorktree first (bar nested phase worktrees); the fallbacks cover its refusal and no-op. |
| Removing the worktree the session stands in | Leave in a standalone call first (ExitWorktree, or cd alone with nothing chained), then remove. A cd chained into the remove moves only the shell, so the session's cwd is deleted and every later tool call is refused until a restart. That includes the merge report, the branch delete, and memory writes. The pretooluse-guard-worktree-remove hook denies the literal-path form. |
| Deleting branch before removing worktree | delete-merged-branch refuses (SKIP) a branch still checked out. Remove worktree first. |
Using --delete-branch on gh pr merge | Fails in worktree flows. Delete branch manually after. |
Treating gh pr merge --auto as blocking | It returns once auto-merge is enabled, not merged. Wait with caliper-wait-pr --merged before cleanup. |
| Polling for a bot comment naming HEAD | Bots may post nothing for a no-op push (e.g. rebase-only), so the wait never ends. Wait on check completion (caliper-wait-pr) instead. |
Preceded by: pr-review (or manual review)
Auto-invoked by: orchestrate — in pr-merge workflow mode
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
/caliper:pr-mergefrom caliper
npx claudepluginhub nikhilsitaram/claude-caliper --plugin caliper19 skills in this plugin
/caliper-settingsUse when the user wants to view, change, or reset their persistent preferences, or when triggered by "/caliper-settings", "my settings", "change defaults"./codebase-reviewUse when asked to audit a codebase, find DRY/YAGNI/complexity issues repo-wide, or perform periodic code quality review/conversateUse when the user invokes /conversate, or asks to talk a change through with you — your recommendations, their decisions, open questions — before anything gets built./coordinateUse when the user wants a queue of issues worked in parallel handoff panes, one PR each, with you as coordinator — "/coordinate", "run these issues in parallel panes", "coordinate issues X, Y and Z", "be the coordinator", "start a release train"./designUse when creating features, building components, adding functionality, or modifying behavior - before any creative or implementation work begins/design-reviewUse when a design doc has been written and before draft-plan is dispatchedOther plugins' skills for the same job