# Git worktrees for Claude Code: run parallel agents without collisions

> A git worktree is a second checkout of the same repository, in its own folder and on its own branch. Give each Claude Code session one and they can't overwrite each other's files. Create one with git worktree add, or let Claude Code do it with claude --worktree, then merge each branch back like any other.

Source: https://gettenfold.app/guides/claude-code-worktrees · By Romain Delage, maker of Tenfold · Published 2026-11-20 · Updated 2026-11-20

Running two Claude Code sessions in the same folder works until both touch the same file. Then one agent's edit lands in the middle of the other's work, a test run picks up half of someone else's change, and you spend the afternoon untangling it. Git worktrees solve this with plain git. This guide covers the manual way, Claude Code's built-in `--worktree` flag, the gotchas, and how to merge the work back.

## What is a git worktree?

The [git documentation](https://git-scm.com/docs/git-worktree) puts it simply: "A git repository can support multiple working trees, allowing you to check out more than one branch at a time." The folder you cloned is the main worktree. Each extra one is a linked worktree: a separate folder with its own files, its own checked-out branch and its own staging area.

Everything else is shared. All worktrees use the same `.git` data, so they see the same commits, branches, remotes and stash. A commit made in one worktree is immediately visible from the others, with no push or fetch needed. That's what makes merging back easy.

For Claude Code, that means each session can edit, build and test in its own folder while all the branches live in one repository.

## How do I run Claude Code in a git worktree by hand?

Start from your main checkout. The repository needs at least one commit. Create one worktree per task, each on a new branch:

```sh
cd ~/code/myapp
git worktree add -b fix-login ../myapp-fix-login main
git worktree add -b search-api ../myapp-search-api main
```

Each command makes a sibling folder, creates the branch from `main` and checks it out there. To use a branch that already exists, leave out `-b`:

```sh
git worktree add ../myapp-bugfix fix-issue-456
```

Then open a terminal tab per worktree and start Claude Code in it:

```sh
cd ~/code/myapp-fix-login
claude -n fix-login
```

`-n` names the session, and the name shows in the terminal tab, which helps once you have several. To see every worktree and the branch it has checked out:

```sh
git worktree list
```

## Does Claude Code have its own worktree flag?

Yes. Anthropic's [worktrees guide](https://code.claude.com/docs/en/worktrees) documents `--worktree`, or `-w`:

```sh
claude --worktree feature-auth
```

This creates a worktree in `.claude/worktrees/feature-auth/` at your repository root, on a new branch named `worktree-feature-auth`, and starts Claude in it. Run it again with another name in another tab for a second isolated session. If you leave out the name, Claude Code makes one up, such as `bright-running-fox`. Add `.claude/worktrees/` to your `.gitignore` so these folders don't show up as untracked files in your main checkout.

A few details from the docs worth knowing:

- **It branches from your default branch, not your current one.** By default, new worktrees start from the repository's default branch on the remote, usually `main`. To start from your current local `HEAD` instead, set `"worktree": { "baseRef": "head" }` in your settings. You can't set it to a branch name, so for a specific branch use `git worktree add`.
- **It can start from a pull request.** `claude --worktree "#1234"` fetches that pull request from `origin` and branches from it. Quote the argument so your shell doesn't treat `#` as a comment.
- **It needs workspace trust.** If you've never run `claude` in the repository, run it once and accept the trust dialog first.
- **It cleans up on exit.** If the worktree has no changes and the session wasn't named, Claude Code removes the worktree and its branch. If there's work in it, or the session was named, it asks whether to keep it. Keep it, and Claude Code prints a `claude --worktree <name> --resume` command to come back later. Runs with `-p` don't clean up.
- **It keeps the session inside.** While a session is isolated in a worktree, Claude Code blocks file edits that target the main checkout, and commands that run in it or point git at it.

You can also ask Claude mid-session to "work in a worktree", and it creates one itself. Custom subagents can get one each with `isolation: worktree` in their frontmatter.

Which to use?

| | `git worktree add` | `claude --worktree` |
|---|---|---|
| Folder | Anywhere you choose | `.claude/worktrees/<name>/` |
| Branch | Any name, from any commit | `worktree-<name>`, from the default branch or `HEAD` |
| `.env` and other ignored files | You copy them | Copied if listed in `.worktreeinclude` |
| Cleanup | `git worktree remove` | Prompt on exit, automatic when clean and unnamed |

## What should you watch out for?

**Dependencies aren't there.** A worktree is a fresh checkout of tracked files. There's no `node_modules`, virtual environment or build output until you install or build in that folder. Budget the disk space and the install time for each one.

**Ignored files aren't copied.** `.env`, `.env.local` and local config files stay in your main checkout. With `claude --worktree`, list them in a `.worktreeinclude` file at the project root, in `.gitignore` syntax:

```text
.env
.env.local
config/secrets.json
```

Only files that match a pattern and are also gitignored get copied. With `git worktree add`, copy them yourself, for example `cp .env ../myapp-fix-login/`.

**Ports collide.** Two dev servers started from two worktrees both reach for the same default port. Give each worktree its own port, through whatever setting or environment variable your dev server reads.

**Databases are shared.** If every worktree points at the same local database, a migration one agent runs changes the data under the others. Use a separate database name per worktree, or run each against its own container.

**Lockfiles conflict.** When two agents each add a dependency, both branches change the lockfile. Expect a conflict when the second one merges. Resolving it usually means taking one side and running your package manager's install again, rather than editing the lockfile by hand.

**A branch can only be checked out once.** Git refuses to check out a branch that's already checked out in another worktree, including your main checkout:

```text
fatal: 'main' is already used by worktree at '/Users/you/code/myapp'
```

Create a new branch with `-b`, or use `--detach` for a throwaway worktree with no branch.

**The stash and branches are shared.** A `git stash` made in one worktree shows up in all of them, and deleting a branch deletes it everywhere.

**Some Claude Code state is shared too.** Choosing "Yes, and don't ask again" in a worktree session saves the rule to the main checkout's `.claude/settings.local.json`, so it applies in every worktree of the repository.

**Cleaning up.** Remove a worktree with git, not the Finder:

```sh
git worktree remove ../myapp-fix-login
```

Git refuses if the worktree has uncommitted or untracked files, so this can't lose work by accident. Add `--force` when you're sure. If you already deleted the folder by hand, `git worktree prune` clears the leftover records. Removing a worktree doesn't delete its branch.

## How do I merge a worktree's work back?

Because branches are shared, you merge from the main checkout like any other branch. First commit in the worktree:

```sh
cd ~/code/myapp-fix-login
git add -A
git commit -m "Fix login redirect"
```

Then bring it into `main`:

```sh
cd ~/code/myapp
git merge fix-login
```

If `main` moved on while the agent worked, settle conflicts in the worktree first, so `main` is never left half-merged:

```sh
cd ~/code/myapp-fix-login
git merge main        # or: git rebase main
# fix conflicts, run the tests, commit
cd ~/code/myapp
git merge fix-login
```

If your team works through pull requests, push the branch instead with `git push -u origin fix-login` and open a pull request from it.

Merge one branch at a time and run your tests after each. Two branches that pass on their own can still break together. When a branch is in, clean up:

```sh
git worktree remove ../myapp-fix-login
git branch -d fix-login
```

## How Tenfold handles worktrees

Tenfold, the Mac app I make for running many Claude Code agents side by side, builds this workflow in. When you start an agent with ⌘N, you choose where it works: **This folder**, or **Its own copy**, which is a git worktree on its own branch. If another agent already works in that repository, Tenfold marks **Its own copy** as recommended. It suggests a branch named after the agent's buddy and the task, like `sprig/clear-drafts-sign`, and puts the copy under `~/.tenfold/worktrees/<repo>/<branch>`, outside your project folder.

When two agents share one checkout and edit the same file, Tenfold flags both. When they work in separate copies, it still compares their branches and tells you before either lands, with a chip like "Will clash with Pogo".

To bring an agent's work into main, pick how it goes in: as one tidy change, step by step, or with a join point. Tenfold checks for clashes with main first. If there are any, you settle them side by side in the agent's own copy, and main isn't touched. It can test the result on a fresh copy of main before it lands. Afterwards it removes the agent's copy and keeps a backup of the branch for 30 days. If main is protected on GitHub, it opens a pull request instead.

Tenfold also recognizes sessions running in worktrees you made yourself, including ones from `git worktree add` and `claude --worktree`, so it can offer to bring their work into main too. See [Run multiple Claude Code agents](https://gettenfold.app/run-multiple-claude-code-agents) and the [Parallel agents docs](https://gettenfold.app/docs/parallel-agents).

## Questions

### Does Claude Code support git worktrees?

Yes. Run `claude --worktree <name>`, or `claude -w <name>`, and Claude Code creates a worktree under `.claude/worktrees/<name>/` on a new branch named `worktree-<name>`, then starts the session in it. You can also start Claude Code in any worktree you made yourself with `git worktree add`.

### Where does claude --worktree put the worktree?

In `.claude/worktrees/<name>/` at the root of your repository, on a branch named `worktree-<name>`. Add `.claude/worktrees/` to your `.gitignore` so the worktrees don't show up as untracked files in your main checkout.

### How do I copy my .env file into a Claude Code worktree?

Add a `.worktreeinclude` file to your project root that lists it, using `.gitignore` syntax. Claude Code copies files that match a pattern and are also gitignored into every worktree it creates. For worktrees you make with `git worktree add`, copy the file yourself.

### Why does git say my branch is already used by another worktree?

Git checks out each branch in only one worktree at a time. If the branch is checked out anywhere else, including your main checkout, `git worktree add` refuses. Create a new branch with `-b`, or use `--detach` for a throwaway worktree.

### How do I delete a git worktree?

Run `git worktree remove <path>`. Git refuses if the worktree has uncommitted or untracked files, so you don't lose work by accident; add `--force` if you're sure. If you already deleted the folder by hand, run `git worktree prune` to clear the leftover records. The branch stays until you delete it with `git branch -d`.

### Do I need a worktree to run two Claude Code agents in one repository?

Not strictly, but it's the safe way. Two agents in one folder edit the same files, and one agent's half-finished change can break the other's build or tests. A worktree gives each agent its own files and branch. [Tenfold](https://gettenfold.app/run-multiple-claude-code-agents) warns you when two agents in one checkout edit the same file, and can start an agent in its own copy.

## Read next

- https://gettenfold.app/run-multiple-claude-code-agents
- https://gettenfold.app/guides/ai-agents-overwriting-files
- https://gettenfold.app/review-ai-code
- https://gettenfold.app/guides/agentic-development-environment

---

Tenfold is a native Mac app for Claude Code, with live code review, git and the notch built in. https://gettenfold.app/
