---
title: "Set up a repo for worktrees"
url: "/docs/guides/set-up-worktrees"
description: "Give every new worktree a setup script, your gitignored files and its own ports"
---

# Set up a repo for worktrees

A fresh worktree is a bare checkout: no `.env`, no installed dependencies, nothing running. This guide makes 10nx finish the job every time it creates one, so Claude starts in a worktree that's ready to run.

Use this guide when a new worktree needs the same few steps by hand before anything works in it.

## Before you start

You need:

- A repo open in 10nx.
- The commands your project uses to install dependencies (`npm install`, `uv sync`, `bundle install`…).
- To know which gitignored files a checkout needs: usually `.env` files, sometimes local certificates or config.

## 1. Write a setup script

Add `scripts.setup` to the repo's `.10nx/settings.toml` (create the file if it isn't there):

```toml
[scripts]
setup = "npm install && cp -n .env.example .env"
```

It runs in the new worktree's folder, after the files are copied and the ports reserved, through your login shell. For anything longer than a line, keep a script in the repo and call it:

```toml
[scripts]
setup = "./scripts/worktree-setup.sh"
```

A non-zero exit marks the setup failed. It has 15 minutes before 10nx stops it. It can't ask you anything: there's no keyboard attached.

## 2. Choose the files to copy

By default 10nx copies gitignored files matching `.env*` from the main checkout into the new worktree. To copy more, set `files_to_copy`:

```toml
files_to_copy = [".env*", "config/local.yml", "certs/"]
```

Patterns use `.gitignore` syntax. Only gitignored files are copied, since committed ones are already in the checkout, and a file the worktree already has is never overwritten. A `.worktreeinclude` file at the repo root wins over this setting, the same way it does for Claude Code. See [Files to copy](../reference/files-to-copy.md).

## 3. Use the worktree's ports

Each worktree gets a block of 10 free ports. The first is in `TNX_PORT`, so start servers on it:

```toml
[scripts.run.dev]
command = "npm run dev -- --port $TNX_PORT"
label = "Dev server"
per_worktree = true

[[preview_urls]]
label = "App"
url = "http://localhost:$TNX_PORT"
```

The other nine are `$((TNX_PORT + 1))` to `$((TNX_PORT + 9))`. Two worktrees never share a block, so their servers don't clash. The **Open** buttons from `preview_urls` show in the Setup view and on the worktree's right-click menu.

## 4. Clean up on remove (optional)

`scripts.archive` runs before 10nx removes a worktree, for stopping a database container or deleting a temp folder:

```toml
[scripts]
archive = "docker compose down"
```

Set `git.delete_branch_on_remove = true` to delete the worktree's branch too, and `git.branch_prefix = "you/"` to name new branches `you/<name>`.

## 5. Commit the file

Commit `.10nx/settings.toml` so everyone who uses the repo gets the same setup. Keep anything only you need in `.10nx/settings.local.toml`.

## Check it worked

1. Press **Cmd+Shift+N** and create a worktree.
2. The Setup view shows each step: create the worktree, copy gitignored files, reserve 10 ports, run the setup script, with the script's output beside them.
3. When it says ready, Claude opens in the worktree. In its terminal, `echo $TNX_PORT` prints the first port.

If the setup fails, the Setup view shows the output and offers **Rerun setup** and **Ask Claude to fix it**.

## Worktrees 10nx didn't create

A worktree Claude or `git worktree add` made in `.claude/worktrees/` didn't get any of this. Right-click its pill in the worktree strip and choose **Run setup**: it copies missing files, reserves ports if it has none, and runs the script.

## Next steps

- [Scripts](../reference/scripts.md): setup, run and archive in detail
- [Environment variables](../reference/environment-variables.md): every `TNX_*` variable
- [Add actions to the Run menu](run-menu.md)
