Give every coding agent its own git worktree. A worktree is a second working folder attached to the same repository, on its own branch, so two agents can edit, build and test one repo at the same time without touching each other's files.
For agents, a worktree per task beats a clone per task almost every time. It's cheaper on disk, and every agent sees the others' commits the moment they land. The catch is a handful of gotchas, covered below along with the commands.
What is a git worktree?
git worktree add creates a new folder with its own checked-out branch. It shares the repository's history and object store with your main checkout, so there is only one .git, and a commit made in any worktree is visible from all of them at once.
# a new branch in a sibling folder, starting from main
git worktree add -b fix-login ../shop-fix-login main
# see every worktree of this repo
git worktree list
# remove it once the branch is merged
git worktree remove ../shop-fix-login
Why do parallel agents need separate worktrees?
Two agents in one folder share one set of files. While one runs the tests, the other is halfway through an edit, and the first sees a broken build it didn't cause. They can stage each other's changes, switch the branch under each other, or both rewrite the lock file. A worktree per agent takes the shared state away: each gets its own files, its own index and its own branch.
| One shared checkout | A worktree per agent | A clone per agent | |
|---|---|---|---|
| Files on disk | Shared | Separate | Separate |
| Branch | One, switched under each other | One per worktree | One per clone |
| Git history | Shared | Shared, one .git | A full copy each |
| Extra disk | None | The working files | The whole repository again |
| See each other's commits | Yes | Yes, at once | After a push and a fetch |
How do you run agents in worktrees by hand?
For two Claude Code sessions on one repo, make two worktrees and start one session in each:
git worktree add -b agent/readme ../shop-readme main
git worktree add -b agent/tests ../shop-tests main
cd ../shop-readme && claude # first terminal
cd ../shop-tests && claude # second terminal
Each session sees only its folder. When both are done, review each branch, merge it like any other branch, and remove the worktree.
What goes wrong with worktrees?
- Untracked files stay behind. A new worktree starts from a commit, without
node_modules,.envor build output. Install dependencies in each one and copy the env files the app needs. - Disk use adds up. Every worktree holds its own working files and, once installed, its own dependencies.
- One branch, one worktree. git refuses to check out a branch that is already checked out in another worktree. Give every agent its own branch.
- Stale entries. Deleting a worktree folder by hand leaves git's record of it.
git worktree pruneclears those. - Sandboxes and commits. A worktree's git data lives in the main checkout's
.git, outside the worktree. An agent sandboxed to its folder can fail togit commit. Codex under "Read & edit files" is one example. Commit from the branch afterwards, or give that task full access.
How does Sidefleet give every task its own worktree?
In Sidefleet, every task gets a worktree and a sidefleet/<id> branch by default. Settings calls this the Own branch workspace. The worker creates it from the repo's HEAD, runs the agent inside it and leaves your checkout alone, so several tasks on one repo run side by side, on one machine or across several.
For the untracked-files problem, add a .sidefleet/worktree.json to the repo, committed or kept locally on each machine:
{"copy": [".env.local"], "setup": "pnpm install --frozen-lockfile"}
copy takes paths that git ignores and copies them into each new worktree. setup runs once per worktree. On APFS, btrfs and XFS the copies are cheap clones; on other file systems each one is a full copy, so prefer setup for large folders. The agent can read anything you copy in, so list secrets on purpose.
When a task needs the installed dependencies or local config of your real checkout, choose In place instead. It works in the checkout itself, one task per repo at a time.
Can you undo what an agent did in its worktree?
Yes. Sidefleet snapshots every message's files before and after, as git refs under refs/sidefleet/snapshots/. Restore to before puts the worktree back to how it was before any message, and the restore can itself be undone. It puts back files, not the conversation: the agent still remembers what it did. From any message you can also fork a new thread with a different agent or model, on its own branch, starting from the files as they were at that point.
How do old worktrees get cleaned up?
Sidefleet keeps worktrees until you press Clean up on the Machines page. It lists what would go and what would stay before it removes anything. Only worktrees with no uncommitted or untracked files, whose commits are on the default branch or were merged as their pull request, are removed. The branch and the snapshots stay, and a follow-up message rebuilds the worktree.
Questions
Does a worktree copy the whole repository?
No. It shares the object database and history with the main checkout. Only the working files are new.
Can two agents work on the same branch?
Not in two worktrees at once. Give each agent its own branch and merge them, or have the second agent start from the first one's branch when it is done.
How do I merge an agent's work?
Like any branch. In Sidefleet you read the diff on the task's page, then merge the sidefleet/<id> branch or open a pull request from it.
Is a worktree a security boundary?
No. It keeps agents from editing each other's files, but an agent with shell access can still read and run anything the user can. For limits on what agents run, see running Claude Code unattended, safely.