Create a git worktree that is ready to work in (dependencies installed, local env files copied, the app on its own free ports), and remove it when done. Use when the user asks for a worktree, and suggest one, asking first, whenever new work would collide with the current checkout: uncommitted changes unrelated to the new task, a dev server or another agent session already running in this folder, two tasks or parallel subagents at once, reviewing or testing someone else's branch or PR without ...
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 Worktree?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/artberri-worktree)
---
name: worktree
description: "Create a git worktree that is ready to work in (dependencies installed, local env files copied, the app on its own free ports), and remove it when done. Use when the user asks for a worktree, and suggest one, asking first, whenever new work would collide with the current checkout: uncommitted changes unrelated to the new task, a dev server or another agent session already running in this folder, two tasks or parallel subagents at once, reviewing or testing someone else's branch or PR without disturbing current work, running two versions of the app side by side, or a risky upgrade or migration that should be easy to throw away. Creating or switching a branch on its own is plain git."
argument-hint: "[task or branch | pr <number> | clean <name>]"
---
# Worktree
A worktree is a second working folder of the same repository, on its own branch, so one piece of work can't disturb another. This skill makes one that is *ready*: the project's local files copied, its dependencies installed, its servers on ports of their own. It reads two things that the `setup-worktree` skill writes into the project: a setup script, and a "Worktrees" section in the instruction file that lists every server, its ports and how to move them.
`<skill-dir>` below is the folder this `SKILL.md` is in.
Pick the job:
- **The user asked for a worktree, or said yes to one**: steps 1 to 3, then 4 if the app has to run.
- **You noticed a collision** before starting a task: [Suggest one](#suggest-one).
- **The user is done with one**: [Clean up](#clean-up).
- **"Which worktrees do I have?"**: `git worktree list`, and the servers you started in each.
## Suggest one
A worktree helps when the new work would collide with what is already in this folder:
- `git status` shows uncommitted changes that have nothing to do with the new task;
- something is already running here: a port from the Worktrees table is in use (`<skill-dir>/scripts/free-port.sh` moves off it), a framework's dev lock file exists (`.next/dev/lock` and the like), or files changed in the last few minutes that you didn't touch (`find . -mmin -5 -type f -not -path './.git/*'`), which means another session is working here;
- the user wants two things done in parallel, or parallel subagents that edit files;
- the user wants to review or test someone else's branch or pull request while keeping their own work as it is;
- the user wants two versions of the app running side by side;
- the task is a risky experiment (a major upgrade, a migration) that should be easy to throw away.
Say in two or three sentences what collides and what you propose: the worktree's name, its base, and what you would run in it. Then ask, and wait. Create nothing until the user says yes; on a no, carry on in the current folder. A plain request to create or switch a branch is no collision: do it with git.
## 1. Read the project's setup
Work out the **main checkout**, the first entry of `git worktree list --porcelain`, and read its instruction file (`AGENTS.md`, `CLAUDE.md`) for a "Worktrees" section. It names the setup script and holds the server table.
**No section, or its script is missing**: stop and offer to run `setup-worktree` first. It studies the project once and writes files to commit, so every later worktree, and every person and agent on the project, gets the same setup. On a yes, read `../setup-worktree/SKILL.md` (relative to `<skill-dir>`), follow it to the end, and come back here. If that file isn't there, tell the user to install the skill and run `/setup-worktree`. On a no, carry on this once with best-effort discovery: the install command from CI or the lockfile, the env files from `.gitignore` and `git status --ignored`, the servers and ports from the project's scripts and configs (`../setup-worktree/references/ecosystems.md` helps when it's there). Copy and install by hand in step 3, and tell the user plainly which commands and ports were *guessed*.
## 2. Create
**Name and place.** A short kebab-case name from the task (`fix-invoice-totals`, `pr-88`), at `<main checkout>/.claude/worktrees/<name>`, where Claude Code's own tools look for worktrees. If that folder already exists for the same work, offer to reuse it. Check that `.claude` and `.claude/worktrees` are real folders (Claude Code refuses a symlinked path), and that `git check-ignore -q .claude/worktrees/<name>` succeeds; if it doesn't, add `.claude/worktrees/` to `.git/info/exclude`, which stays local, and mention that `setup-worktree` adds it to `.gitignore`.
**Base.** Choose it on purpose, and say which one you used:
| The work | Base | Create with |
| --- | --- | --- |
| New and unrelated to the current branch | the default branch on the remote, fetched now | `git fetch origin <default>`, then `git worktree add -b <branch> <path> origin/<default>` |
| Builds on local work, including unpushed commits | local `HEAD` (uncommitted changes stay behind) | `git worktree add -b <branch> <path> HEAD` |
| An existing branch | that branch | `git worktree add <path> <branch>`; only on the remote: `git worktree add --track -b <branch> <path> origin/<branch>` |
| A pull request | its head | `git fetch origin pull/<n>/head:pr-<n>` (GitLab: `merge-requests/<n>/head`), then `git worktree add <path> pr-<n>` |
The default branch is `git symbolic-ref --short refs/remotes/origin/HEAD`. Name a new branch the way the project names branches (its docs, `git branch -a`), or `worktree-<name>` as Claude Code does. Git refuses a branch that is already checked out in another folder; offer a new branch from it instead. When the task might need unpushed work and you can't tell, ask which base.
## 3. Get it ready and go in
Run the setup script from inside the worktree: `cd <path>`, then the script as the Worktrees section writes it. It copies the gitignored files the project needs from the main checkout and installs the dependencies; read its output. When it fails, stop and show the error: the worktree stays, so the user can look.
Then move in:
- **Claude Code:** call `EnterWorktree` with `path` set to the worktree's absolute path. Claude Code then blocks commands in the main checkout until `ExitWorktree`.
- **Other agents:** run every command with the worktree as the working directory, and tell the user `cd <path>` to work there themselves.
Report the path, the branch, the base (ref, short sha, and why), and what the setup did.
## 4. Run the app
Only when the task needs it running (comparing, testing by hand) or the user asks.
1. From the Worktrees table, take each server you need and every port in its row, secondary ones included. `<skill-dir>/scripts/free-port.sh <port>...` prints a free replacement for each, in order, never a default: the main checkout may start on its defaults at any time.
2. Read the section's shared-state line before starting. When the copy would share a database, a volume or a cache with the main checkout, say so, and use the separation the section gives.
3. Start each server in the background from the worktree, with every port moved the way the table says, and its output in `"$(git rev-parse --git-dir)/<server>.log"`, which goes away with the worktree. Keep each PID.
4. Wait until each port answers (up to a minute); when one doesn't, show the end of its log.
5. Report each URL (`http://localhost:<port>`) next to the main checkout's, and the secondary ports you moved.
## 5. Work in it
- After a pull, a rebase or a branch switch in the worktree, run the setup script again: it reinstalls only when a lockfile or toolchain pin changed.
- Run checks (lint, type check, tests) with the project's own commands, from inside the worktree.
- For parallel subagents, make one worktree per subagent here, and give each its absolute path and ports in its prompt. A Claude Code subagent with `isolation: worktree` gets a fresh worktree from Claude Code instead, so tell it to run the setup script first.
## Clean up
1. **Stop the servers** you started there: the PIDs you kept, or whatever listens on the worktree's ports (`lsof -t -iTCP:<port> -sTCP:LISTEN`).
2. **Leave it.** In Claude Code, inside the worktree, call `ExitWorktree` with `action: "keep"`: it never removes a worktree entered by `path`, and git commands in the main checkout stay blocked until you leave.
3. **Look for work that would be lost**, from the main checkout:
- `git -C <path> status --porcelain`: uncommitted and untracked files;
- `git log --oneline <branch> --not --remotes`: commits on no remote;
- gitignored files the worktree changed or added, other than dependencies and build output (an edited `.env`): removing the worktree deletes them.
When there is any, list it and ask what to do (push, commit, copy out, or throw away), and wait. Use `--force` or `-D` only after the user said to throw that work away.
4. **Remove it**: `git worktree remove <path>`. When git says it is locked (Claude Code locks the worktrees it makes for agents), ask before `git worktree unlock`.
5. **The branch.** Delete it with `git branch -d <branch>` once it is merged into the default branch. A squash-merged pull request (`gh pr view <branch> --json state` says `MERGED`) makes `-d` refuse; then `-D` is safe, and say why. Otherwise keep the branch and say so, or ask.
6. **A folder deleted by hand** shows as `prunable` in `git worktree list`: `git worktree prune` clears it.
Report what was stopped, removed and kept.