Skip to content
86% off$349 $48.86code BF2026

Guides

Claude Code Worktrees: Run Agents Without Conflicts

Set up Claude Code worktrees with exact git commands, plus copying .env, installing dependencies, separate dev-server ports, merging and cleanup.

GuideUpdated 11 min read

On this page

Claude Code worktrees are ordinary git worktrees. To give a session its own copy of your repo, run git worktree add -b fix-login ../myapp-fix-login main from your project, then cd ../myapp-fix-login, copy your .env, install dependencies, and start claude there. Each session gets its own folder and branch, so two agents can't overwrite each other's files or switch branches under each other.

Creating the worktree is one command. Getting the agent able to run your app takes more: secrets, dependencies, a free port, and cleanup afterwards. Below is each step with plain git, followed by how Sledbot does the same steps for you.

What a git worktree is and why Claude Code agents need one

A git worktree is an extra working folder attached to the same repository. Each worktree has its own checked-out branch and its own files on disk. All worktrees share one history and object store, so a commit made in one is immediately visible from the others.

Running two Claude Code sessions in the same folder causes predictable problems:

  • Both agents edit the same files, and one silently undoes the other's work.
  • If one agent runs git checkout or git switch, the branch changes for the other agent too.
  • Test runs, build folders and dev servers collide.
  • git diff shows both agents' changes mixed together, so you can't review them separately.

Branches and stashes don't fix this, because they still share one working tree. A worktree gives each agent a separate set of files on its own branch, which you review and merge on its own.

For the broader workflow, see how to run multiple Claude Code agents in parallel. If you haven't set up the CLI yet, start with installing Claude Code on a Mac.

Create a worktree for each Claude Code session

  1. Open Terminal in your main checkout:

    bash
    cd ~/Developer/myapp
  2. Create a new branch and a worktree folder next to the repo:

    bash
    git worktree add -b fix-login ../myapp-fix-login main

    This creates the branch fix-login from main and checks it out into ../myapp-fix-login. To undo it, see the cleanup section below.

  3. Move into the worktree and start Claude Code:

    bash
    cd ../myapp-fix-login
    claude
  4. Check what exists at any time:

    bash
    git worktree list

    This prints every worktree with its path, current commit and branch.

To check out a branch that already exists instead of creating one, leave out -b:

bash
git worktree add ../myapp-review feature-x

One branch, one worktree. Git won't check out a branch that is already checked out in another worktree, including your main checkout. Give every session its own branch.

Where to put worktrees

  • Sibling folders (../myapp-fix-login) are the simplest choice. Nothing inside your repo changes, and editors and search tools in the main checkout don't see the copies.

  • A folder inside the repo (for example .worktrees/fix-login) keeps everything together, but you must hide it from git so it doesn't show up as untracked. Add it to your local exclude file:

    bash
    echo ".worktrees/" >> .git/info/exclude

    This ignores the folder for you only, without touching the shared .gitignore. To undo it, delete that line from .git/info/exclude.

Branch names can contain slashes (agent/fix-login), but keep folder names flat (myapp-agent-fix-login) so paths stay readable.

Copy .env and run setup in each worktree

A new worktree contains only files that git tracks. Anything ignored is missing: .env, .env.local, node_modules, build output and local config files. So an agent's first test run in a fresh worktree often fails on missing config or modules.

Copy the secrets from the main checkout:

bash
cp ../myapp/.env .

You could symlink instead (ln -s ../myapp/.env .env). A symlink keeps every worktree in sync, but if an agent edits the file, every worktree sees the change. A copy keeps each worktree independent, which is usually what you want with agents.

Then install dependencies inside the worktree, with whatever your project uses:

bash
npm ci

Each worktree needs its own install. With npm, that means a separate node_modules in every worktree, each taking its own disk space.

Watch for shared state that worktrees don't separate:

  • Databases. If every worktree points at the same local dev database, a migration run by one agent changes it for all of them. Use a separate database name per worktree when agents touch the schema.
  • Docker Compose. By default Compose names the project after the folder, so containers from different worktrees don't clash by name (unless your compose file sets fixed names). Published host ports still clash.

To save typing, wrap the steps in a shell function in ~/.zshrc:

zsh
wt() {
  local branch="$1"
  local root
  root="$(git rev-parse --show-toplevel)" || return
  local dir="${root}-${branch//\//-}"
  git worktree add -b "$branch" "$dir" || return
  [ -f "$root/.env" ] && cp "$root/.env" "$dir/"
  cd "$dir" && npm ci
}

Open a new Terminal window (or run source ~/.zshrc), then run wt fix-login from the main checkout. It creates the branch from whatever the main checkout has checked out, makes a sibling folder, copies .env if there is one, and installs dependencies. Replace npm ci with your own setup command. To remove the function, delete it from ~/.zshrc and open a new Terminal window.

Give each worktree its own dev-server port

Two dev servers can't both listen on port 3000. The second fails to start, or quietly picks another port that the agent doesn't know about. Assign a port per worktree and tell the agent which one to use.

Most dev servers accept a port flag or read a PORT variable. Vite and Next.js both accept --port:

bash
npm run dev -- --port 3001

If your server reads the environment instead:

bash
PORT=3001 npm run dev

Check your framework's docs for which one applies. Then put the port in your first prompt, for example "the dev server for this worktree runs on port 3001", so Claude Code opens and tests the right URL.

Two things break even with separate ports:

  • Cookies. Browsers scope cookies by host, not by port. Logging in on localhost:3001 can overwrite the session cookie for localhost:3000. Use a separate browser profile or private window per worktree if you test logins.
  • Callback URLs. OAuth redirect URLs and CORS allowlists often name one port. Add the extra ports to your dev configuration, or the login flow fails only in the new worktrees.

Merge or open a PR from the worktree

First see what the agent changed. From inside the worktree:

bash
git status
git diff main...HEAD

git status shows uncommitted edits. git diff main...HEAD shows everything committed on the branch since it left main. Commit any leftover changes in the worktree before you merge.

To merge locally, go back to the main checkout. It must be on the branch you're merging into:

bash
cd ~/Developer/myapp
git switch main
git merge --no-ff fix-login

--no-ff keeps a merge commit, so each agent's work stays grouped in the history. You don't need to check out fix-login in the main checkout to merge it, which is good, because git won't let you while the worktree holds that branch.

To open a pull request, push from the worktree and use the GitHub CLI:

bash
git push -u origin fix-login
gh pr create --fill

--fill takes the title and body from the branch's commits. This needs gh installed and signed in.

Clean up worktrees and branches

Old worktrees keep their files, their dependencies and their branch. They add up fast when agents run every day.

  1. Remove the worktree folder and git's record of it:

    bash
    git worktree remove ../myapp-fix-login

    Git refuses if the worktree has uncommitted changes or untracked files. Commit or copy anything you want to keep first.

  2. Delete the merged branch:

    bash
    git branch -d fix-login

    -d only deletes a branch that is merged. Git also won't delete a branch that is still checked out in a worktree.

  3. If you deleted a worktree folder by hand (in Finder or with rm), clear the stale record:

    bash
    git worktree prune

    This removes entries for worktree folders that no longer exist. It doesn't touch any files.

The following commands throw away work that isn't committed. Use them only when you're sure the agent's changes are worthless:

bash
git worktree remove --force ../myapp-fix-login
git branch -D fix-login

--force removes the worktree even with uncommitted changes. -D deletes the branch even if it was never merged.

Automate Claude Code worktrees in Sledbot

Doing all of the above by hand is fine for one or two sessions a week. Running several agents a day means repeating the same steps for every task. We built Sledbot for that. It's a native Mac app (macOS 14 or later) that runs Claude Code and other coding agents in real terminals, by default each in its own worktree.

Start an agent in a worktree

  1. Add your repo as a space: File ▸ Add Project Folder… (⌘O, Command-O), or drag the folder onto the sidebar.
  2. Press ⌘T (Command-T) to open New task, and describe the task.
  3. Leave Work in a new worktree on. It's on by default for git repos. Edit the branch name (it starts with agent/…) or click Use suggested name.
  4. Pick Claude Code as the agent if it isn't selected already. Sledbot defaults to the agent you used last, and to Claude Code before that.
  5. Press ⌘↩ (Command-Return) to start.
Sledbot New task page with a prompt typed, the chips row and Work in a new worktree switched on
Describe the task, pick an agent and start it in its own worktree.

The worktree goes in <repo>/.sledbot/worktrees/, named after the branch with / replaced by - (so agent/fix-login becomes agent-fix-login). Sledbot adds .sledbot/ to .git/info/exclude, so it never shows up as untracked. Closing a running agent stops it but keeps its worktree and files.

The starting agents doc covers the other options on this page.

Describe copy, setup and ports in one file

Put a .sledbot/config.json (or sled.json) at the repo root:

json
{"setup": ["npm ci"], "copy": [".env"], "run": "npm run dev -- --port $SLED_PORT", "archive": "docker compose down"}
  • copy lists ignored files to bring into a worktree, such as .env.
  • setup lists commands that prepare the worktree, such as installing dependencies.
  • run is your dev-server command.
  • archive runs when a thread is archived, for example to stop containers.

Terminals in a worktree get these environment variables:

VariableWhat it holds
SLED_PORTA port for this worktree; SLED_PORT_1 to SLED_PORT_9 give extra ones
SLED_WORKTREEThe worktree's path
SLED_ROOTThe main repo's path
SLED_BRANCHThe worktree's branch
SLED_CONTEXT<worktree>/.sledbot/context, a git-excluded folder

Because the run command uses $SLED_PORT, each worktree's dev server gets its own port without you numbering them.

Every Sledbot terminal has the sled command on its PATH. sled worktree create makes a worktree from a branch (--branch), a GitHub issue (--issue N) or a pull request (--pr N), and runs setup unless you pass --no-setup. sled worktree setup, run and archive run the matching scripts on demand.

Review, merge or open a PR

Click Review changes on the agent to see every change against the base branch, hunk by hunk. Press A to accept (stage) a hunk, R to reject it (restore the original), or C to send a comment with the file and line to the agent's terminal.

Merge and Open PR become available once every hunk is reviewed:

  • Merge commits any uncommitted changes in the worktree, then runs git merge --no-ff --no-edit in your main checkout. The main checkout must be on the base branch.
  • Open PR commits, runs git push -u origin <branch> and gh pr create --fill, then opens the PR. This needs the GitHub CLI.

More detail is in reviewing an agent's changes.

Keep agents inside their worktree

Sledbot's built-in approval rules ask you before an agent writes files or runs rm -r outside its worktree (temp folders excepted). For Claude Code the action waits until you answer, through a hook Sledbot adds for that launch only. Your global Claude config isn't changed.

Clean up

Right-click the space and choose Clean up worktrees…, then Scan. Select items under Sledbot worktrees and choose Move to Trash. In the confirmation, tick Also delete the branches of merged worktrees (git branch -d) to remove the branches too. Automatic cleanup can remove merged worktrees after 7 days, but that rule is off until you turn it on. See Insights, Resources and Cleanup.

Sledbot cleanup confirmation for Sledbot worktrees with one unpushed worktree to tick and the delete merged branches option

Sledbot is part of MacSuite ($349 one-time as of November 2026), and you can try it free for 7 days. If you're comparing tools that manage worktrees for agents, see Conductor alternatives.

Which method to use

Plain git commandsShell functionSledbot
Create worktree and branchOne commandOne commandFrom New task, on by default
Copy .env, install dependenciesBy handScriptedPer-repo copy and setup
Separate dev-server portPick by handPick by hand$SLED_PORT per worktree
Review and mergegit diff, git merge, ghSamePer-hunk review, Merge, Open PR
Cleanupgit worktree remove, git branch -dSameCleanup scan, optional daily cleanup
CostFreeFreePart of MacSuite

Pick plain git if you run a second session now and then. Pick the shell function if you do it often and only need setup automated. Pick Sledbot if you run several agents a day and want ports, review and cleanup handled too.

If it doesn't work

  • Git says the branch is already checked out. A branch can be in only one worktree at a time, including your main checkout. Create a new branch with -b, or remove the other worktree first.
  • Tests fail with missing config or modules. Ignored files don't come with a new worktree. Copy .env and run your install command inside the worktree.
  • The dev server says the port is in use. Another worktree's server holds it. Start this one on a different port and tell the agent the new URL.
  • git worktree remove refuses. The worktree has uncommitted changes or untracked files. Commit what you want to keep, then remove it. Use --force only if you're sure.
  • git worktree list shows a folder you already deleted. Run git worktree prune.
  • The merge went into the wrong branch. git merge merges into whatever the main checkout has checked out. Run git switch main (or your base branch) before merging.
  • Claude Code edited your main checkout. It was started from the wrong folder. Quit it, cd into the worktree, and start claude again.

FAQ

Does each worktree take a full copy of the repo? Each one has a full copy of the working files, but they share one git history and object store, so the .git data isn't duplicated. Dependencies like node_modules are installed separately in each worktree and are usually the larger cost.

Can two Claude Code sessions share one worktree? They can, but you're back to the original problem: both edit the same files and you can't review their work separately. Give each session its own worktree.

Can I open the worktree in my editor? Yes. A worktree is a normal folder, so you can open it in any editor while the agent works. Your edits and the agent's land in the same files, so avoid editing the same file at the same time.

Every app.
One purchase.