Skip to content
Back to skills

Worktree

ASecurity

Create a Parallel Worktree

  • 7 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added December 4, 2026
ai-agentsshellnodegitapidocumentation

Works with

  • claude code
  • terminal
  • api

Security analysis

A92/100
  • mediumInstalls packages at runtime which could introduce malicious dependencies

Pro shows the line behind each finding and how to fix it

Scanned December 4, 2026

npx -y skills add mayankmankhand/llm-peer-review --skill worktree --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Worktree?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Worktree
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/mayankmankhand-worktree/badge)](https://www.skillsdirectory.com/skills/mayankmankhand-worktree)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: worktree
description: "Create a Parallel Worktree"
---

# Create a Parallel Worktree

Set up an isolated worktree so you can work on a feature in a separate editor window or terminal without affecting your main working copy.

**Use this when:** You want to work on multiple features in parallel, or isolate experimental work from your main branch.
**Don't use this when:** You're doing a quick fix or documentation update on main.

<rules>

- Do NOT rollback or undo any steps if something fails partway through. Report the failure and move on. Attempting to undo a partially created worktree can leave git in a tangled state.
- Do NOT read the contents of `.env.local` - it contains API keys. Use the shell `cp` command to copy it.
- This command takes no arguments. Worktree names are auto-generated. The branch gets a meaningful name later when `/explore` identifies an issue.

</rules>

## Procedure

<procedure>

### Step 1: Guards

Run these checks. If any fail, stop with the indicated message.

1. **Git repo check** - Run `git rev-parse --git-dir` (M16). A non-zero exit means this is not a repository; stop: "This isn't a git repository. Navigate to your project root and try again."
2. **Not already a worktree** - Run `git rev-parse --path-format=absolute --git-dir --git-common-dir` (M16). It prints the git directory for this working copy, then the shared git directory for the whole repo, both absolute so a subfolder of the main copy still compares correctly. Git older than 2.31 prints the flag back as an extra first line; compare the last two lines. Two different lines mean you're in a worktree. Stop: "You're already in a side copy of the project. Run this command from your main project folder instead."
3. **Not detached HEAD** - Run `git symbolic-ref HEAD` (M16). A non-zero exit means detached HEAD; stop: "You're in detached HEAD state. Check out a branch first with `git checkout main`."
4. **Uncommitted changes warning** - Run `git status --porcelain`. If there is output, warn: "You have changes not yet saved as a checkpoint. They won't appear in the new side copy, because only saved checkpoints are copied." Do NOT stop - just inform the user.

### Step 2: Setup

1. Run `git worktree prune` to clean up stale entries.
2. Run `mkdir -p .claude/worktrees` to ensure the directory exists.

### Step 3: Find the next number

1. Parse `git worktree list` output and scan `.claude/worktrees/` for existing folders.
2. Look for folders matching the pattern `worktree-N` (simple numeric only - ignore issue-named folders like `worktree-58-branch-conflicts`).
3. Find the highest N and use N+1, by one pipeline over both sources (M16): `{ git worktree list --porcelain | sed -n 's#^worktree .*/worktree-\([0-9]\+\)$#\1#p'; ls .claude/worktrees 2>/dev/null | sed -n 's#^worktree-\([0-9]\+\)$#\1#p'; } | sort -n | tail -1 | awk '{print $1+1}'` prints the next N, and nothing when none exist, so start at 1.

### Step 4: Create the worktree

Run: `git worktree add -b worktree-N .claude/worktrees/worktree-N HEAD`

If it fails because the branch name is taken, increment N and try again.

Then record the branch's cut point, the commit it was cut from: `node .agents/tk/scripts/merge-check.js --record-cut worktree-N` (M16). It writes the commit into the branch's git config, where `/merge` reads it when the branch lands, and prints it as `cut`. When `HEAD` was not on main, the cut is not on main either, and `/merge` will say so before landing. If the record fails, warn the user but do NOT stop: `/merge` then finds the cut another way, as a weaker check.

### Step 5: Install dependencies

Install host project deps; toolkit deps depend on how the toolkit is installed. Decide every install and copy step here and in Step 6 by command (M16): `W=.claude/worktrees/worktree-N; for p in $W/package.json $W/.claude/scripts/package.json .env.local $W/.env.local CODEBASE_MAP.md $W/CODEBASE_MAP.md .claude/settings.local.json $W/.claude/settings.local.json; do test -e "$p" && echo "present $p" || echo "absent $p"; done` prints one `present` or `absent` line per file, in the order the steps read them.

1. **Host project deps** - if `package.json` exists at the worktree root, run: `npm install --prefix .claude/worktrees/worktree-N`. Skip if there is no host package.json.
2. **Toolkit deps** - two cases:
   - **Plugin install (the normal case):** nothing to do. The toolkit's dependencies live with the plugin under `~/.claude/plugins/`, installed once per machine when the plugin is installed and shared by every worktree and every project.
   - **Copy-install (a `.claude/scripts/package.json` exists in the worktree):** run `npm install --prefix .claude/worktrees/worktree-N/.claude/scripts`. Without it, `/review-browser`, `/ask-gpt`, and `/ask-gemini` will not work in the new worktree. Skip if there is no toolkit package.json.

If either install fails, warn the user but do NOT stop. The worktree is still usable for general work, but the affected toolkit features will not run until the missing deps are installed manually.

### Step 6: Copy environment

Copy `.env.local` from the main repo root into the worktree root, so the worktree keeps the project's keys. The API scripts take each key from a real environment variable first, then the project's own `.env.local` (searched from the working folder up to the git root, which in a worktree is the worktree root, so the main repo's copy is out of reach), then `~/.claude/plugins/.env.local`:
`cp .env.local .claude/worktrees/worktree-N/.env.local`

- If `.env.local` does not exist in the main repo, skip and note it.
- If it already exists in the worktree, skip (don't overwrite).

Copy `CODEBASE_MAP.md` from the main repo root into the worktree root the same way:
`cp CODEBASE_MAP.md .claude/worktrees/worktree-N/CODEBASE_MAP.md`

- If `CODEBASE_MAP.md` does not exist in the main repo, skip and note it -
  /explore in the worktree will detect the missing map and generate one
  automatically via /index (one-time, may take a minute).
- If it already exists in the worktree, skip (don't overwrite).

Copy `.claude/settings.local.json` from the main repo into the worktree the same way, so the worktree keeps the permission rows the main copy already allows. Git ignores that file, so a new worktree starts without it, and the plugin's setup command in Claude Code runs in the main copy only (it stops in a worktree), so nothing else writes it there. Make the folder first, as a call of its own, then copy:
`mkdir -p .claude/worktrees/worktree-N/.claude`
`cp .claude/settings.local.json .claude/worktrees/worktree-N/.claude/settings.local.json`

- If `.claude/settings.local.json` does not exist in the main repo, skip and note it.
- If it already exists in the worktree, skip (don't overwrite).

### Step 7: Print summary

</procedure>

<reference>

Print a clear summary using this format:

```
Your side copy of the project is ready!

  Path:         /full/absolute/path/to/.claude/worktrees/worktree-N
  Branch:       worktree-N
  Host npm:     installed (or: failed - run manually / skipped - no package.json)
  Toolkit npm:  plugin - nothing to install (or: installed / failed - run manually / skipped - no .claude/scripts/package.json)
  .env:         copied (or: skipped - not found / already exists)
  Map:          copied (or: skipped - not found / already exists)
  Settings:     copied (or: skipped - not found / already exists)
  Cut:          <short sha> recorded (or: not recorded - <why>)

Next steps:
  1. Stay in this session and work in the side copy from here. Its copy of the toolkit's packages is not installed: `npm install --prefix .agents/tk/scripts` there needs the user's yes.
  2. Use /explore to start working on your issue
     (/explore will rename the branch once an issue is identified)
  3. When done, run /document to create a PR and clean up
  4. Then /merge <PR number> lands it on main, checked for lost work
```

</reference>

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.