Run Claude Code agents in parallel with git worktrees: create a worktree per task, start an agent in each, merge back, and clean up. Tested commands, real output, and the limits.

Two Claude Code sessions in one folder will edit the same files. One is halfway through a refactor when the other rewrites the module underneath it. Git worktrees fix that by giving each agent its own folder and its own branch, from the same repository.
This guide walks through the whole loop: create a worktree, run an agent in it, merge the work back, and clean up. Every command below was run on Git 2.47, and the output shown is what Git printed.
What a worktree is
A worktree is a second checkout of the same repository in a different folder. It shares history and remotes with your main folder, but it has its own files and its own branch. An agent working in one worktree cannot touch the files in another.
Git has shipped worktrees for years. You do not need to install anything.
Step 1: Create a worktree for each task
From your project folder, run git worktree add with a path and a new branch name. Here are two tasks, each in a folder next to the project:
git worktree add ../my-app-login -b add-login-page
git worktree add ../my-app-payments -b fix-payment-bug
Git creates the folder, creates the branch, and checks it out:
Preparing worktree (new branch 'add-login-page')
HEAD is now at d8b6f02 Initial commit
The new folder contains your tracked files and nothing else. Anything in .gitignore stays behind: no .env, no node_modules. Copy your environment file and install dependencies in each worktree before you start an agent there.
Step 2: Run Claude Code in each worktree
Open one terminal per worktree and start Claude Code in each:
cd ../my-app-login
claude
Give each session one task. The agent in my-app-login builds the login page on add-login-page. The agent in my-app-payments fixes the bug on fix-payment-bug. They work at the same time and never see each other's edits.
One rule to know: a branch can only be checked out in one worktree. If you try to switch your main folder to a branch an agent is using, Git refuses:
fatal: 'add-login-page' is already used by worktree at '/Users/you/code/my-app-login'
That refusal is the safety net doing its job.
Step 3: See what is running
git worktree list shows every worktree, the commit it is on, and its branch:
git worktree list
/Users/you/code/my-app d8b6f02 [main]
/Users/you/code/my-app-login 2904d85 [add-login-page]
/Users/you/code/my-app-payments 8d600df [fix-payment-bug]
This tells you where each branch lives. It does not tell you whether an agent is still working, waiting on you, or done. You find that out by checking each terminal.
Step 4: Merge the work back
When an agent finishes, review its changes and commit them in the worktree. Then go back to your main folder and merge the branch like any other:
cd ../my-app
git merge add-login-page
Updating d8b6f02..2904d85
Fast-forward
login.js | 1 +
1 file changed, 1 insertion(+)
create mode 100644 login.js
Merge the second branch the same way. If two agents changed the same lines, you get a normal merge conflict here, at a moment you chose, instead of two agents overwriting each other mid-task. Most teams will push the branch and open a pull request instead of merging locally. The worktree does not change that workflow.
Step 5: Remove the worktree
Once the branch is merged, remove the worktree and delete the branch:
git worktree remove ../my-app-login
git branch -d add-login-page
If the agent left uncommitted or untracked files behind, Git stops rather than delete them:
fatal: '../my-app-login' contains modified or untracked files, use --force to delete it
Check what is there. If you do not need it, run git worktree remove --force ../my-app-login. If you deleted a worktree folder by hand, git worktree prune clears the stale record.
The shortcut: claude --worktree
Claude Code can do steps 1 and 2 in one command. Per Anthropic's worktree documentation, --worktree (or -w) with a name creates a worktree and starts Claude in it:
claude --worktree feature-auth
This is what the documentation says happens:
- The worktree is created under
.claude/worktrees/feature-auth/at your repository root, on a new branch namedworktree-feature-auth. - If you leave the name off, Claude generates one.
- New worktrees branch from the repository's default branch on the remote, not from whatever you have checked out. A
worktree.baseRefsetting changes that to your localHEAD. - A
.worktreeincludefile in your project root lists gitignored files, such as.env, to copy into each new worktree. - On exit, Claude removes a clean, unnamed worktree automatically. If the worktree has work in it, Claude asks whether to keep or remove it.
The documentation also recommends adding .claude/worktrees/ to your .gitignore, so worktree contents do not show up as untracked files in your main checkout. And it is direct about setup: a worktree is a fresh checkout, so you still install dependencies in each one.
Run the same command with a different name in a second terminal and you have a second isolated session. You can also ask Claude to "work in a worktree" during a session.
Where worktrees stop
Worktrees isolate files. That is all they isolate, and it is worth being honest about the rest.
Ports are still shared. Each worktree is a folder, not a machine. If your dev server listens on port 3000, the second worktree to start it gets an "address already in use" error. You end up assigning ports by hand and remembering which is which.
Every worktree needs its own install. Dependencies and build output are not tracked, so each new worktree starts without them. On a large project, that is a full install and a second copy of node_modules for every task.
There is no shared view for review. Five agents means five terminals. git worktree list shows branches, not progress. To review, you open each folder in an editor or read diffs in each terminal, one at a time.
For two parallel tasks, none of this hurts much. At five, the bookkeeping is a job of its own.
Where DevSwarm helps
DevSwarm is built on the same idea, with the bookkeeping handled. Every DevSwarm workspace is a git worktree on its own branch, created when you create the workspace. You pick which local files, such as .env, come along. Port mapping is handled for you, so each workspace can run its own dev server (how port variables work).
Each workspace also gets a full VS Code editor scoped to its worktree, and all your workspaces sit in one sidebar with their branch and git status. You review an agent's diff, commit, and push from the same window. And each workspace can run a different agent: Claude Code in one, any of the other 18 supported agents in the next.

If you want the longer argument, read why worktrees aren't enough and four ways to run multiple Claude Code agents in parallel. The features page shows the rest.
Worktrees by hand are a good skill to have. When you are ready to stop managing them, download DevSwarm and create your first two workspaces.

