Skip to content
Skill

Gh PR merge worktree checkout trap

Diagnoses and works around gh pr merge failing on a 'main already used by worktree' error

Gh PR merge worktree checkout trap is an open-source agent skill for developer tools. It is available on Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, Windsurf and Goose.

Scripts
None
Platforms
Claude Code, Codex, Cursor
GitHub stars
5
Last commit
License
MIT
Listing updated

Similar skills

About Gh PR merge worktree checkout trap

This skill helps an agent recognise a specific gh CLI failure: when another git worktree has main checked out, gh pr merge can report a worktree error even though the merge landed on GitHub. The agent is told to confirm the real state with gh pr view, checking mergedAt for post-merge failures and autoMergeRequest for auto-merge enable failures, before deciding whether anything actually went wrong.

It also covers cleanup after the error. The guidance covers removing the leftover worktree before deleting the feature branch, deleting remote refs only after a merge is confirmed, and checking whether the remote branch still exists. A GitHub REST API merge is offered as a way to avoid the local checkout step altogether.

The skill expects the gh CLI authenticated against the repository, git with worktrees in use, and a shell the agent can run commands in. It ships as a single markdown file with no scripts.

When your agent uses it

Diagnose and bypass `gh pr merge --squash --delete-branch` failing with "failed to run git: fatal: 'main' is already used by worktree at ..." when another git worktree has main checked out. Use when: (1) you run gh pr merge and see this exact error, (2) you have multiple worktrees in the repo (e.g. `.claude/worktrees/*`), (3) the error appears even though the GitHub merge itself looks fine. The merge SUCCEEDED on GitHub — only gh's local-side effect (post-merge `git checkout main`) failed. Verify via `gh pr view N --json state,mergedAt`; if state=MERGED, you're done. This also applies to `gh pr checkout` and any other `gh` subcommand that tries to touch the local main branch while another worktree has it claimed. v1.2.0 (2026-05-26) adds the sequential-error variant: if you re-run from the main-repo worktree after the first error, you can then hit "cannot delete branch <feature-branch> used by worktree at <feature-worktree>" — same one-checkout-per-branch invariant, this time applied to the feature branch. Cl

From the skill's SKILL.md. Your agent reads this description to decide when to load the skill.

What it can access

From a static check of its 1 file (14 KB) on GitHub on 4 December 2026. Nothing was run. Read the files before you install a skill: it can tell your agent to run anything its tools allow.

Check another skill before you install it.

Install

This skill ships in the agent-traffic-control plugin. In Claude Code, add the marketplace and install the plugin (it may include other skills too):

/plugin marketplace add wan-huiyan/agent-traffic-control
/plugin install agent-traffic-control@wan-huiyan-agent-traffic-control

Or copy only this skill to ~/.claude/skills/gh-pr-merge-worktree-checkout-trap:

tmp=$(mktemp -d) &&
git clone --depth 1 https://github.com/wan-huiyan/agent-traffic-control.git "$tmp" &&
mkdir -p ~/.claude/skills &&
rm -rf ~/.claude/skills/gh-pr-merge-worktree-checkout-trap &&
cp -R "$tmp/plugins/agent-traffic-control/skills/gh-pr-merge-worktree-checkout-trap" ~/.claude/skills/gh-pr-merge-worktree-checkout-trap &&
rm -rf "$tmp"

For one project only, put the folder in .claude/skills/gh-pr-merge-worktree-checkout-trap inside it instead.

Claude Code docs on skills

Copy the skill folder to ~/.agents/skills/gh-pr-merge-worktree-checkout-trap to use it in every repository:

tmp=$(mktemp -d) &&
git clone --depth 1 https://github.com/wan-huiyan/agent-traffic-control.git "$tmp" &&
mkdir -p ~/.agents/skills &&
rm -rf ~/.agents/skills/gh-pr-merge-worktree-checkout-trap &&
cp -R "$tmp/plugins/agent-traffic-control/skills/gh-pr-merge-worktree-checkout-trap" ~/.agents/skills/gh-pr-merge-worktree-checkout-trap &&
rm -rf "$tmp"

For one repository only, put the folder in .agents/skills/gh-pr-merge-worktree-checkout-trap inside it instead.

Codex docs on skills

Copy the skill folder to ~/.cursor/skills/gh-pr-merge-worktree-checkout-trap to use it in every project:

tmp=$(mktemp -d) &&
git clone --depth 1 https://github.com/wan-huiyan/agent-traffic-control.git "$tmp" &&
mkdir -p ~/.cursor/skills &&
rm -rf ~/.cursor/skills/gh-pr-merge-worktree-checkout-trap &&
cp -R "$tmp/plugins/agent-traffic-control/skills/gh-pr-merge-worktree-checkout-trap" ~/.cursor/skills/gh-pr-merge-worktree-checkout-trap &&
rm -rf "$tmp"

For one project only, put the folder in .cursor/skills/gh-pr-merge-worktree-checkout-trap inside it instead.

Cursor docs on skills

Run in a terminal (add --scope workspace to install it for the current workspace only):

gemini skills install https://github.com/wan-huiyan/agent-traffic-control.git --path plugins/agent-traffic-control/skills/gh-pr-merge-worktree-checkout-trap

Or copy the folder to ~/.gemini/skills/gh-pr-merge-worktree-checkout-trap yourself.

Gemini CLI docs on skills

For GitHub Copilot in VS Code, copy the skill folder to ~/.copilot/skills/gh-pr-merge-worktree-checkout-trap:

tmp=$(mktemp -d) &&
git clone --depth 1 https://github.com/wan-huiyan/agent-traffic-control.git "$tmp" &&
mkdir -p ~/.copilot/skills &&
rm -rf ~/.copilot/skills/gh-pr-merge-worktree-checkout-trap &&
cp -R "$tmp/plugins/agent-traffic-control/skills/gh-pr-merge-worktree-checkout-trap" ~/.copilot/skills/gh-pr-merge-worktree-checkout-trap &&
rm -rf "$tmp"

For one project only, put the folder in .github/skills/gh-pr-merge-worktree-checkout-trap inside it instead.

VS Code docs on skills

Get the folder:

git clone --depth 1 https://github.com/wan-huiyan/agent-traffic-control.git

Then copy agent-traffic-control/plugins/agent-traffic-control/skills/gh-pr-merge-worktree-checkout-trap into your agent's skills folder as gh-pr-merge-worktree-checkout-trap. Agents that support SKILL.md read one folder per skill.

The SKILL.md format

What it does

Error diagnosis
Explains why gh reports the main-branch worktree error after a merge has already succeeded on GitHub.
Merge state verification
Shows how to confirm a PR is merged with gh pr view, including the auto-merge check.
Safe worktree cleanup
Orders removal of worktrees and deletion of local and remote branches to avoid a second error.
API merge fallback
Describes merging a pull request through the GitHub REST API to bypass the local checkout.
Stacked PR warnings
Warns that deleting a base or head branch too early can auto-close open pull requests.

Good for

  • Why did gh pr merge fail with a worktree error when the PR looks merged?
  • Clean up the leftover feature worktree and branch after a squash merge
  • Merge a pull request through the API when main is checked out in another worktree
  • Check whether a PR with auto-merge enabled actually registered on GitHub
  • Remove the stale remote branch after a merge that printed a worktree error

Gh PR merge worktree checkout trap FAQ

What triggers Gh PR merge worktree checkout trap?

Running gh pr merge and seeing the error that 'main' is already used by a worktree, especially when multiple worktrees exist in the repository.

Does Gh PR merge worktree checkout trap change my repository on its own?

No. It describes commands to run, such as removing worktrees, deleting branches and deleting remote refs, but the agent runs them only as the task calls for.

Does Gh PR merge worktree checkout trap need any scripts or extra tools?

It ships no scripts. It relies on the gh CLI and git being available in the environment.

Will the API merge delete the remote branch?

According to the skill, the direct API merge does not delete the remote branch, so a separate delete call or the repository's auto-delete setting is needed.

How do I install Gh PR merge worktree checkout trap in Claude Code?

Copy the skill folder from GitHub to ~/.claude/skills/gh-pr-merge-worktree-checkout-trap to use it in all your projects, or to .claude/skills/gh-pr-merge-worktree-checkout-trap in one project. Or install the agent-traffic-control plugin, which includes it: /plugin marketplace add wan-huiyan/agent-traffic-control, then /plugin install agent-traffic-control@wan-huiyan-agent-traffic-control.

Does Gh PR merge worktree checkout trap run scripts?

No. It is instructions only, with no scripts. This comes from a static check of the files on GitHub; nothing was run. Read the files before you install it.

Which AI agents can use Gh PR merge worktree checkout trap?

Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, Windsurf and Goose. Each reads skills in the SKILL.md format from its own folder; the install steps on this page list them.

What are the best alternatives to Gh PR merge worktree checkout trap?

The closest alternatives to Gh PR merge worktree checkout trap are Gh PR merge unstable state needs auto and watch branch deletes, Gha PR merge ref shows upstream changes, Merge PR, Git post merge cleanup and Gh PR create orchestration cwd wrong head.

Where this comes from

  • Description: from the skill's own SKILL.md.

More from wan-huiyan/agent-traffic-control