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 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:
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:
git worktree add ../myapp-bugfix fix-issue-456
Then open a terminal tab per worktree and start Claude Code in it:
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:
git worktree list
Does Claude Code have its own worktree flag?
Yes. Anthropic’s worktrees guide documents --worktree, or -w:
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 localHEADinstead, set"worktree": { "baseRef": "head" }in your settings. You can’t set it to a branch name, so for a specific branch usegit worktree add. - It can start from a pull request.
claude --worktree "#1234"fetches that pull request fromoriginand branches from it. Quote the argument so your shell doesn’t treat#as a comment. - It needs workspace trust. If you’ve never run
claudein 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> --resumecommand to come back later. Runs with-pdon’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:
.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:
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:
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:
cd ~/code/myapp-fix-login
git add -A
git commit -m "Fix login redirect"
Then bring it into main:
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:
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:
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 and the Parallel agents docs.