Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Merge?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/mayankmankhand-merge)
---
name: merge
description: "Land Worktree PRs"
---
# Land Worktree PRs
**Use this when:** One or more PRs from worktree branches are finished and you want them on main, in an order you choose, with proof that no work was lost on the way.
**Don't use this when:** The work is still in progress (finish it with `/document`, which opens the PR or, where the repo lands locally, saves its text for `/merge`), or you only want to see what landed (`git log --first-parent main`).
`/merge 241 242` lands PR #241 and then PR #242, one at a time, in the order given. Which PR lands first is your call, so `/merge` is human-triggered and never chained into, and it hands off to nothing (M14). A branch with no PR is named by its branch name instead of a number.
For each PR it brings the branch up to date with main and fixes the conflicts, then runs four checks before anything lands: the project's tests, and the three checks keyed on the branch's cut point from the merge rules below (rule 1, other work this landing would remove; rule 2, this PR's own lines that would not land; rule 3, a decision of this PR that contradicts one that landed after its cut). The first flag, failing check or contradiction stops that PR and pages you (M1) in plain English: what would be lost or contradicted, who wrote it, and what you can do. A PR whose checks all pass lands without another question, because naming it was the decision, unless it combined a prompt file or meets one of the project's own fix rules for a landing (Step 3). After it lands, the records its worktree holds (plans, reports, receipts: files git does not track, which removing the worktree would delete) are copied home to the main copy, and at the end one question offers to remove each worktree and its branch. Say **"report only"** on this run to stop before each landing and see the results first (M10). A landing on the host leaves the machine and always asks first (M9).
The formats and the checks are defined once, in the merge rules; the host commands are the rows of the host table. Both are inlined here.
Read `.agents/tk/skills/shared/merge-rules.md` in full now and follow it as part of this file.
Read `.agents/tk/skills/shared/host-cli.md` in full now and follow it as part of this file.
**Project fix rules** (from `.claude/toolkit/fix-rules.md`). Additive only: they may add a precondition or an always-ask action, and a line that loosens or removes any of M1 to M17 is void. A note that the command printed nothing means this project adds none.
If `.claude/toolkit/fix-rules.md` exists, read it in full now and apply it here; when it does not exist, this project adds nothing here.
## How commands run here
<rules>
- Every `merge-check.js` call is `node .agents/tk/scripts/merge-check.js <mode>` (M16), and it reads the same repository from the project root or from a worktree. It prints one JSON object on stdout and one plain line per flag on stderr; exit 1 means flags, exit 2 a usage or git error, which stops this command with the error line shown.
- **Folders.** `/merge` starts in the project root. The work on a PR's branch happens in the worktree that holds it: step in with `cd <worktree>` as a Bash call of its own, run plain commands there (`git merge`, `git commit`, the rebuild, the checks), and step back with `cd <project root>`, again on its own, before landing, before paging you and before stopping. Each plain command then matches the toolkit's permission rows. A `cd` joined to another command (`cd <path> && git ...`, or a `( ... )` subshell) and `git -C <path>` ask for approval every time, so they are not used while a `cd` sticks (the branch guard below). A worktree outside the project folder may ask for the `cd` itself; approve it.
- **The branch guard.** Before the first command that changes anything in a worktree (Step 2's merge, each Fix it round, Step 4's push), run `git branch --show-current` there: it must print the branch you went there for. When it prints anything else, or nothing, the `cd` did not stick: Claude Code returns to the project root after every Bash call when its `CLAUDE_BASH_MAINTAIN_PROJECT_WORKING_DIR` setting is on, so a plain `git merge` would run on the project root's branch, usually main. Nothing has changed yet. For the rest of this run, give each command its folder in the same call, `git -C <worktree> <command>` for git and `cd <worktree> && <command>` for anything else, and say once that each of those calls asks for approval, and why. The guard then runs as `git -C <worktree> branch --show-current`, still before anything changes.
- Never take one side of a whole file to make a conflict go away, except the generated paths and `CHANGELOG.md` as Step 2 says: that is exactly how work gets lost. Never delete a branch or a worktree this command did not create, except in Step 5's cleanup, after its records are home and you say yes.
- Temporary files (the landing message, the decisions file, a changelog block, a checks file) go in a fresh folder from `mktemp -d /tmp/merge-text.XXXXXX`, made per "Temporary folders" in `.agents/tk/skills/shared/html-outputs.md`.
</rules>
## Procedure
<procedure>
### Step 1: Before landing anything
1. **Host and style.** Detect the host with the host table's rule. When that rule takes an answer from you (the remote names neither host and both `gh` and `glab` are installed), add `--host gh` or `--host glab` to every later `merge-check.js` call, because the script cannot ask. Then fetch when the repo has an `origin` remote (`git fetch origin`), and run `merge-check.js --style`. Its `main` is the default branch and its `style` the repo's landing habit; say the style in one line ("This repo lands each branch by merging it on this machine, so `/merge` merges locally and pushes nothing."). On `unknown`, ask once which of the three styles to use and record the answer with `merge-check.js --style <answer>`. A host style (`host-merge`, `host-squash`) passes `--main origin/<main>` to every later `merge-check.js` call, and `<main>` in Steps 1.3, 2, 3 and 4.1 means `origin/<main>` too, because the host's copy of main is the one that moves and a fetch leaves the local branch where it was; a `main` that `--style` printed with `origin/` already in front takes no second one. `local-merge` uses `<main>` as printed. A host style lands through the host, so ask once now, for the whole run, before anything is pushed: "This run pushes and merges PRs #241 and #242 on GitHub. Go?" (M9).
2. **The PRs.** With no arguments, ask which PRs to land and in what order; never pick the order yourself. For each number, run `merge-check.js --pr <N>`: it reads the PR through the host's read row and prints `title`, `state`, `branch`, `head`, `decisions`, `changelog` and `cutFrom`. A PR that is not open is skipped, said in one line. A branch that is not local is fetched (`git fetch origin <branch>:<branch>`). A branch named without a PR takes its title from you: ask once, with its newest commit's subject as the suggested answer. The exception is a branch with a landing file, `reports/landing/<branch>.md`, the PR text `/document` saves instead of opening a PR where the repo lands locally. Look in the worktree `git worktree list --porcelain` names for the branch, then in the main copy (that list's first `worktree` line), where `/document` brought the file home because Claude Code deletes a clean worktree when its session ends. A file found in the main copy can be an earlier branch's, because it stays there after that branch lands and `/worktree` can reuse the name, so use it only when the `cutFrom` that `merge-check.js --pr-body <that file>` prints is the `cut` that `merge-check.js --cut <branch>` prints. Wherever the file was found, run `merge-check.js --record-cut <branch>` once you use it, because updating the branch in Step 2 moves a cut that was never recorded, and a retry after the worktree is gone must still match. That file is this branch's PR body. Run `merge-check.js --pr-body <that file>` for its `decisions` and `changelog`, take the title from the file's first line without asking, and pass `--pr-body <that file>` wherever a PR's call passes `--pr <N>` (Steps 2.1 and 3.3).
3. **Landings made without this command.** Run `merge-check.js --marker`. When it prints no marker, run `merge-check.js --mark <main>` and say "No landing record yet: starting it at main's tip (<short sha>); earlier landings are not checked." Otherwise run `merge-check.js --since <marker>`. For each landing whose `rule1` says not checkable and which names a `pr`, read that PR with `merge-check.js --pr <pr>`; when it has a `cutFrom`, check the landing with `merge-check.js --landing --before <landing>^1 --after <landing> --cut <cutFrom> --tip <landing>` (a squash keeps no branch tip, so rule 2 has nothing to compare there). Any flag pages you before more lands: name each line, the file and the landing that removed it, and offer to keep it on purpose or to stop. On "keep it", the flag's `keep` lines go into the first landing message this run writes, or, when the run lands nothing, into an empty commit on main, made in the worktree that holds main the way Step 4 goes there (`git commit --allow-empty -F <file>`). Then `merge-check.js --mark <main>`.
### Step 2: Update and fix, for each PR in order
1. **The cut.** Run `merge-check.js --cut <branch> --pr <N>` (a branch without a PR: `--cut <branch>`, adding `--pr-body <file>` when Step 1 found its landing file). It prints `cut`, `source` and `onMain`. When `onMain` is false, stop this PR and explain: its branch was cut from another branch, not from main, so landing it would also land that branch's unlanded commits; land that branch first, or cut this one again from main. A `merge-base` source is a weaker check: say so in the PR's closing line. Note the branch tip now, `git rev-parse <branch>`: it is this PR's own work, before any fix, and the checks compare against it.
2. **The worktree.** `git worktree list --porcelain` names the worktree whose `branch` line is `refs/heads/<branch>`. When there is one, step into it (`cd <worktree>`): `git status --porcelain --untracked-files=no` must print nothing, because uncommitted changes there are someone's work, so step back and ask. When there is none, make a temporary one, `git worktree add .claude/worktrees/merge-<N> <branch>` (a branch without a PR: its name in place of `<N>`), step into it, and remove it at the end. A temporary worktree starts without `.env.local` and the installed dependencies: before the checks, copy and install them the way `/worktree`'s setup step does. The rest of Step 2, and Step 3's tests, run inside the worktree.
3. **Merge main in.** `git merge --no-ff --no-edit <main>`. When it stops on conflicts, fix each one this way:
- **Generated paths** (the files `.claude/toolkit/generated.json` lists): take main's side, `git checkout --theirs -- <path>`. They are rebuilt below.
- **`CHANGELOG.md`**, when the PR has a `changelog` part: take main's side, `git checkout --theirs -- CHANGELOG.md`; the PR's block goes back in below. A PR without the part keeps both sides of its changelog conflict, like any text.
- **The lessons files** merge with both sides kept when `.gitattributes` says so (C-18). Either way, `grep -oE '^- \*\*[^*]+\*\*' LESSONS.md | sort | uniq -d` prints each lead the index now holds twice (no output: none); merge each such pair into one bullet, and its write-ups in `LESSONS-detail.md` into one.
- **Everything else**: combine both sides, so that the result does what this PR decided and what main's landings decided (their `Decisions:` lines are in `git log <cut>..<main> --first-parent`). When the two cannot both hold (one line, two different values), keep this PR's line so the file still works, and note the line of main's it replaced: the verdict names both, and you choose which one wins.
- **A prompt file** (a command, skill, agent or rules file; M9), or a file every session or stage reads as instructions (`CLAUDE.md` at the root or in `.claude/`, `CLAUDE.local.md`, and each `.claude/toolkit/*.md` other than `README.md` and `adoption.md`), is combined the same way, but the result is a prompt edit no one approved: note each such file, and the verdict pages it (Step 3).
Then `git add` the fixed files and `git commit --no-edit`.
4. **Re-insert and rebuild.** When the PR has a `changelog` part, put main's whole file back whether or not it conflicted, `git checkout <main> -- CHANGELOG.md`, so a release that landed in between cannot hold the block under its version. Write the part byte for byte to `<tmp>/block.md` with the Write tool, run `merge-check.js --changelog-place CHANGELOG.md <tmp>/block.md --write`, which puts the block where the merge rules place it and prints the `rule` it used, and commit the file on its own ("Re-insert the changelog block for #<N>"). Then run each `rebuild` command from `generated.json`, and commit what changed ("Rebuild <paths> after the update").
5. Note the updated tip, `git rev-parse <branch>`: it is exactly what lands.
### Step 3: Check, for each PR
1. **Tests**, inside the worktree, the way `/execute`'s test step runs them: the checks file is `.claude/toolkit/checks.json` when the worktree has one; otherwise write the ecosystem default as a one-entry file with the Write tool (`[{"id": "tests", "check": "npm test --if-present", "expect": {"exit": 0}}]` for a `package.json`, the same row with `pytest -q` when `pyproject.toml` or `pytest.ini` exists), and skip with one line when there is neither, or `package.json` still holds npm's `no test specified` placeholder. Run `node .agents/tk/scripts/run-checks.js --checks <checks file> --out <tmp>/checks --timeout 600000 --full`; it is green when its JSON has `summary.fail` 0 and `summary.error` 0. A skipped run (its JSON carries `skipped`: nothing changed since a run that passed) counts as passing; quote its `skipped.note` when you report that PR. Add `--no-skip` when something the tests read changed outside the tracked files (installed packages, ignored settings, git tags or branches): the skip cannot see those. Read a failure with `grep -nE '^not ok|FAIL|Error' <stdoutFile> | head -30` over the file each failing row names, never the whole log (M16). Then step back to the project root (`cd <project root>`): the checks below run there, so a copy-install runs its own copy of the script, not the branch's.
2. **Lines (rules 1 and 2).** `merge-check.js --landing --before <main> --after <updated tip> --cut <cut> --tip <own tip>`, adding `--kept <file>` when this run kept lines that are not on main yet (one `Kept on purpose:` line each). Its JSON's `before` is the commit of main that was checked. Its JSON also lists `deleted`, the files this landing removes from main, and `untracked`, the ones among them that the branch's own `.gitignore` files ignore: the branch stopped tracking them but means to keep them, and the landing would delete every other checkout's copy. Neither list stops the PR by itself; a project fix rule can (Step 3.4). Name the deleted files in the PR's landing line (Step 4.5), and keep `untracked` for Steps 4 and 5.
3. **Decisions (rule 3).** `merge-check.js --decisions --cut <cut> --tip <own tip> --pr <N> --out <tmp>/decisions.md` (for a branch without a PR, `--pr-body <file>` in its place when Step 1 found its landing file, and nothing otherwise). When its `landings` list is empty, nothing landed after the cut and there is nothing to contradict: skip the dispatch and say so. Otherwise dispatch the `decision-checker` agent (the helper `decision-checker`) with the Agent tool and no model parameter: it is a judge and runs on the session model (`.agents/tk/skills/shared/model-routing.md`). The prompt carries the file's path and nothing else. It returns `No contradictions` or up to six lines `N. <this PR's decision> contradicts <landing>: <why>`; any other reply is dispatched once more, and a second unreadable reply pages as a contradiction would. When the agent type is not found, fall back as `model-routing.md` says.
4. **The verdict.** All clean, no prompt file combined in Step 2, and no project fix rule for a landing that this PR meets: land (Step 4). A combined prompt file pages even when everything else is clean: show each one's combined lines and the line of each side it came from, and land on a yes (M9). A project fix rule that adds a precondition or an always-ask action for a landing (for example, a yes before any landing whose `deleted` list is not empty) pages the same way: name the rule and what in this PR meets it, and land on a yes. A fix rule that names no landing, one written for the fix loops, does not gate a landing. Anything else stops this PR before it lands and pages you, one plain-English line per problem: the line, the file and the landing or commit that wrote it (rules 1 and 2), the failing check and its first error (tests), or the two decisions and why both cannot hold (rule 3, and any conflict the fix could not combine, with the line it kept and the one it replaced). Offer what fits:
- **Fix it**: Claude steps back into the worktree, fixes it on the branch, commits, and runs Step 3 again, at most 3 rounds, then stops and pages again.
- **Keep it on purpose** (a flagged line): the flag's `keep` line goes into the landing message, exactly as printed.
- **Land it as it is** (a contradiction): the PR's decisions go into the landing message as always, so history shows which decision came later.
- **Stop**: nothing of this PR lands; the PRs after it in the list are not started.
### Step 4: Land, for each PR
1. **Main has not moved.** On a host style run `git fetch origin` first, because only a fetch moves `origin/<main>`. `git rev-parse <main>` must still print the `before` commit Step 3 checked; when it does not, go back to Step 2 for this PR.
2. **The landing message**, written with the Write tool to `<tmp>/landing.txt` in the merge rules' format: the subject for the style (`Merge PR #<N>: <title>` for a merge, `<title> (#<N>)` for a squash, `Merge <branch>: <title>` for a branch without a PR), the title, `Decisions:` with the PR's decision list (`- None recorded` when it has none), the `Cut from:` line with the full cut, and every `Kept on purpose:` line from this run. On GitLab with host-merge, the message ends with the line `See merge request <ref>`, the `ref` that `merge-check.js --pr` printed: GitLab's own merge button writes that line, and `--style` reads it to tell a host merge from a local one.
3. **local-merge.** Go to the worktree that holds main (`git worktree list --porcelain` names it; often it is the project root itself; when none holds main, make a temporary one, `git worktree add .claude/worktrees/merge-main <main>`, removed at the end) and step in when it is not the project root. There `git status --porcelain --untracked-files=no` must print nothing, else step back and ask. When this PR's `untracked` list (Step 3.2) is not empty, first copy each of those files that exists in this folder to the run's temporary folder, `cp <file> <tmp>/kept-<n>` with `<n>` counting from 1 across the whole run (the next PR goes on counting, so a copy still waiting to go back is never overwritten), one call each, because the merge deletes them here. Then `git merge --no-ff -F <tmp>/landing.txt <branch>` and confirm `git diff --quiet <updated tip> <main>` exits 0. Copy each saved file back, `cp <tmp>/kept-<n> <file>`, and say so in one line; when a copy back is refused, print the saved path so you can copy it back yourself. Then step back to the project root. Nothing is pushed: the landing is in your local main.
4. **host-merge or host-squash.** Step into the branch's worktree and run the pre-push check for the branch (M11), `node .agents/tk/scripts/pre-push-check.js origin <branch>`, then `git push origin HEAD:<branch>`, and step back; exit 1 pages you and only you can say push anyway, exit 2 means fetch and check again. Then run `merge-check.js --wait --pr <N> --head <updated tip>`: after a push, and after the previous landing, the host needs some seconds before it reports the PR mergeable at that head, and a merge row run sooner fails. It reads the host's status every 3 seconds for up to 90; exit 1 pages you with the status the host still reports. Write the message's body (every line after the subject and the blank line under it) to `<tmp>/landing-body.md`, and run the host table's merge row with `--merge` for host-merge or `--squash` for host-squash, the updated tip as the head guard (`--match-head-commit` on GitHub, `--sha` on GitLab), and the subject single-quoted per the host table's rule. Then `git fetch origin` and confirm `git diff --quiet <updated tip> origin/<main>` exits 0. When it does not (main moved on the host after the check), check the landing itself with `merge-check.js --landing --before <landing>^1 --after <landing> --cut <cut> --tip <own tip>` and page any flag. Then run the tests on that new main before the next PR: `git worktree add --detach .claude/worktrees/merge-main-check origin/<main>`, copy and install there the way Step 2 does for a temporary worktree, the tests there the way Step 3.1 runs them, and remove it the way Step 4.5 removes a temporary worktree. A failing check stops the batch and pages you. When the two match, main is exactly what the tests already ran on, so nothing runs again.
5. **Record it.** `merge-check.js --mark <main tip>` (`origin/<main>` for a host style). Remove this PR's temporary worktree with `git worktree remove .claude/worktrees/merge-<N>`; when git refuses because it holds changed or untracked files (the dependency install can rewrite a lockfile, and the copied `.env.local` is untracked where the project does not ignore it), run it again with `--force`, because this run made that worktree and Step 4 has just confirmed that everything committed in it landed. Never force the removal of a worktree this run did not make, except as Step 5's cleanup says. When a worktree this run did not make holds the branch (Step 2.2), bring its records home: from the project root, `merge-check.js --bring-home <branch>` copies the files git does not track under `plans/` and `reports/`, and the dated pages under `artifacts/html/`, into the main copy, because `git worktree remove` deletes them without a word. It replaces a main-copy file only when it brought that file home itself and nothing changed it since (so a record `/document` already brought home is refreshed with its newer copy), never any other, and keeps each copy's modified time. Exit 0 or 1 means the copy finished (1 lists `differs` and `leftover` paths): keep its `differs`, `leftover`, `ignored` and `needsForce` for Step 5. Exit 2 means it stopped on a git error or a record it could not copy: what it copied stays, and Step 5 offers no removal of that worktree. Then print one line: `#<N> landed as <short sha> (<style>): rules 1 and 2 clean, <n> checks passed, <no contradictions | n kept on purpose>`, ending with `; changelog block <rule>` when Step 2 re-inserted a changelog block, `<rule>` being the rule `--write` printed, with `; deletes <files>` when its `deleted` list is not empty, and with `; <copied> records brought home` or `; records not brought home: <its error line>` when the bring-home ran. Then run `node .agents/tk/scripts/render-html.js --standing --name review --branch <branch>` (M16): when its `open` is above 0, end that line with `; its review page still has <open> open findings: <file>`, then `, last published at <url>` when `url` is not empty (`open` is counted from that file, and the hosted copy can be older). Then fold the branch's cycle page into the main one, `node .agents/tk/scripts/render-html.js --fold-cycle --branch <branch>` (M16): it adds the branch's cycles and the doc mismatches it found to the main copy's cycle page, closes there the ones it fixed, and publishes nothing, so the main page's hosted copy catches up at its next `/document`. When its `folded` is above 0, end that line with `; its cycle page is folded into the main one` and, when its `url` is not empty, ` (<url>)`. Exit 1 means it could not read a page: end the line with its error instead and go on.
### Step 5: Close
One line per PR, landed or stopped and why, then what is left for you: for local-merge, "Nothing was pushed: push main when you are ready (the host marks each PR merged when main reaches it)"; for a host style, when the project root is on `<main>` and `git status --porcelain --untracked-files=no` prints nothing there, `git pull --ff-only origin <main>`, saving and copying back the run's `untracked` files around the pull the way Step 4.3 does, and say main is up to date; otherwise "Your local main is behind until you pull", and for each `untracked` file, that the pull will delete it from this folder, so copy it aside first and put it back after. Remove the temporary main worktree if this run made one (the `--force` rule in Step 4.5 applies).
**The cleanup**, once for the whole run, after everything above. It covers each landed branch whose worktree this run did not make and whose bring-home finished (exit 0 or 1). Removing a worktree deletes your files, so it always asks (M9), in one question for all of them that names, per worktree: its path and branch, how many records were copied and how many were already home, each `differs` path (a record the main copy holds differently: removal deletes this copy), each `leftover` path (a file that is not a record, or a change never committed: removal deletes it), and each `ignored` path, the other ignored files outside the records, such as a build folder or a map built there (removal deletes them; the yes to this question covers them). Ask that any editor window or Claude session open in it be closed first. On yes, for each worktree: `git worktree remove <path>`, adding `--force` when its `needsForce` is true and it lists nothing under `differs` or `leftover` (git refuses a plain removal over untracked files, and each of those is a record already home). When it lists something, `--force` and the removal itself need a yes that names those files; without one, that worktree stays. Then `git branch -d <branch>` from the project root: git refuses when the branch holds a commit main does not have (a squash landing, or work after the landing), so say why in one line and keep the branch. On no, both stay, and the records are home already.
**The map**, once for the whole run, after the cleanup (M12). `/document` never refreshes the codebase map, so this is where it catches up with what landed. Ask only when at least one PR landed in this run, the project root is on `<main>`, and local main now holds the landings: local-merge, or a host style whose pull above ran. Then run `node .agents/tk/scripts/session-init.js` (M16) and read its `map`: ask only when `map.exists` is false or `map.stale` is true (10 or more commits behind). The question is one line: "The codebase map is <map.commitsBehind> commits behind main (or: there is no codebase map yet). Refresh it with `/index` now? It runs several helpers at once." On yes, invoke `/index` through the Skill tool; on no, move on and leave the map as it is. When local main was left behind, or the project root is on another branch, ask nothing and add to the close: "Run `/index` on main, after any pull, if the map is 10 or more commits behind", since a map built now would describe another commit than main's.
</procedure>