# Gh PR merge worktree checkout trap

> Gh PR merge worktree checkout trap is an open-source agent skill for developer tools. Diagnoses and works around gh pr merge failing on a 'main already used by worktree' error. It is available on Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, Windsurf and Goose.

- **Kind:** Skill
- **Pricing:** Open source
- **Platforms:** Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, Windsurf, Goose
- **Website:** https://github.com/wan-huiyan/agent-traffic-control/tree/main/plugins/agent-traffic-control/skills/gh-pr-merge-worktree-checkout-trap
- **Repository:** https://github.com/wan-huiyan/agent-traffic-control
- **GitHub stars:** 5
- **License:** MIT
- **Skill name:** gh-pr-merge-worktree-checkout-trap
- **Collection:** https://github.com/wan-huiyan/agent-traffic-control
- **Categories:** [developer tools](https://ai-powered.com/categories/developer-tools)
- **Listing updated:** 2026-12-04

## About

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

## What it can access

From a static check of the files on GitHub; nothing was run.

- [Instructions only, no scripts](https://github.com/wan-huiyan/agent-traffic-control/tree/HEAD/plugins/agent-traffic-control/skills/gh-pr-merge-worktree-checkout-trap)
- [License: MIT](https://github.com/wan-huiyan/agent-traffic-control/tree/HEAD/plugins/agent-traffic-control/skills/gh-pr-merge-worktree-checkout-trap)

## Features

- **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

## Install

### Claude Code

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):

```sh
/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`:

```sh
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.

### Codex

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

```sh
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.

### Cursor

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

```sh
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.

### Gemini CLI

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

```sh
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.

### VS Code

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

```sh
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.

### Other agents

Get the folder:

```sh
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.

## 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.

## Alternatives

- [Gh PR merge unstable state needs auto and watch branch deletes](https://ai-powered.com/skills/gh-pr-merge-unstable-state-needs-auto-and-watch-branch-deletes): Gh PR merge unstable state needs auto and watch branch deletes is an open-source agent skill for developer tools.
- [Gha PR merge ref shows upstream changes](https://ai-powered.com/skills/gha-pr-merge-ref-shows-upstream-changes): Gha PR merge ref shows upstream changes is an open-source agent skill for developer tools.
- [Merge PR](https://ai-powered.com/skills/merge-pr): Merge PR is an open-source agent skill for developer tools.
- [Git post merge cleanup](https://ai-powered.com/skills/git-post-merge-cleanup): Git post merge cleanup is an open-source agent skill for developer tools.
- [Gh PR create orchestration cwd wrong head](https://ai-powered.com/skills/gh-pr-create-orchestration-cwd-wrong-head): Gh PR create orchestration cwd wrong head is an open-source agent skill for developer tools.
- [Gh](https://ai-powered.com/skills/gh-prathamdby): Gh is an open-source agent skill for developer tools.
- [Git clean worktrees](https://ai-powered.com/skills/git-clean-worktrees): Git clean worktrees is an open-source agent skill for developer tools.
- [Git merge](https://ai-powered.com/skills/git-merge): Git merge is an open-source agent skill for developer tools.

---
Source: https://ai-powered.com/skills/gh-pr-merge-worktree-checkout-trap
