Two coding agents in one folder will edit the same files. One stops halfway through a refactor, and the other's tests break on code it never touched. The fix is old and boring: give each agent its own git worktree.
A worktree is a second checkout of the same repository, in its own folder, on its own branch. All of them share one .git, so a commit made in one can be merged from any other at once. Each agent edits only its own folder.
This post covers the commands, the four things that break, and the tools that do it for you.
Disclosure: I make Trek, one of the tools compared at the end.
1. One worktree per agent
From your repository, make a worktree on a new branch and start the agent in it:
git worktree add ../myapp-login -b fix-login cd ../myapp-login claude In a second terminal, do the same for the next agent:
git worktree add ../myapp-search -b fix-search cd ../myapp-search codex Git won't check out one branch in two worktrees, so each agent needs a new branch. git worktree list shows them all.
2. Copy the files git ignores
A new worktree holds only what is committed. Your .env, .env.local, local certificates and the like stay behind, and most apps won't start without them.
By hand, from your original checkout:
cd ../myapp cp .env .env.local ../myapp-login/ To have it happen every time, use a post-checkout hook. Git runs it inside each new worktree that git worktree add makes, and for a new worktree its first argument is all zeros (40, or 64 in a SHA-256 repository):
#!/bin/sh # .git/hooks/post-checkout (chmod +x it) case "$1" in *[!0]*) exit 0 ;; esac main="$(git worktree list --porcelain | sed -n '1s/^worktree //p')" for f in .env .env.local; do [ -f "$main/$f" ] && [ ! -e "$f" ] && cp "$main/$f" "$f" done # Install dependencies here, for example: npm ci exit 0 Three notes:
- Hooks live in the shared
.git/hooks(or wherevercore.hooksPathpoints), so one hook covers every worktree. - It works with anything that makes worktrees with
git worktree add, including some of the apps below. - Keep the
exit 0at the end. If the hook fails,git worktree addreports a failure even though the folder was made.
If you only use Claude Code or the Codex app, there is a simpler route. List the files in a .worktreeinclude file at the repository root, written like .gitignore:
.env .env.local Both copy the files that match it and that git ignores, when they make a worktree. Two exceptions: Claude Code skips the file when a WorktreeCreate hook makes the worktree (the hook has to copy them), and Codex documents the copying only for worktrees its app manages, so with codex exec --worktree use the hook above.
3. Dependencies and ports
- Dependencies live in each folder. Run your install step in every new worktree (the hook above is a good place). pnpm keeps one store and links packages into each folder, which saves disk.
- Ports: two dev servers in two worktrees both want port 3000. Start each with its own port option, such as
next dev -p 4001orvite --port 4001. Some servers readPORTfrom.env, but Next.js doesn't. - Anything outside the folder is still shared: a local database, a Docker volume, a cache in your home folder. Give each worktree its own database name if the agents run migrations.
4. Merge and clean up
Review each branch like a pull request from a colleague. Then merge it, or push it and open a real pull request:
cd ../myapp # your main checkout, on main git merge fix-login git worktree remove ../myapp-login git branch -d fix-login git worktree remove refuses if the worktree has uncommitted changes or untracked files, which is what you want. It does delete ignored files without asking, including the .env you copied in, so keep nothing only there.
The agents' own flags
Both big agent CLIs can make the worktree themselves:
- Claude Code:
claude --worktree fix-login(or-w) starts it in.claude/worktrees/fix-loginon a new branch,worktree-fix-login. By default it branches from the remote's default branch, and theworktree.baseRefsetting"head"branches from where you are. It copies what.worktreeincludenames. When you exit, an unnamed session's clean worktree is removed by itself. A named session (likefix-loginhere), or one with changes, asks first. Ignored files such as.envdon't count as changes. - Codex:
codex exec --worktree "fix the search bug", or/worktreeinside a session, runs in a managed worktree, and the Codex app has a Worktree option under the composer. Codex keeps those worktrees in$CODEX_HOME/worktreeson a detached HEAD. The app runs a local environment's setup script, kept in the.codexfolder, when it makes a worktree.
These are the simplest choice if you use one agent. Each flag starts only its own agent, though, so for Claude Code on one bug and Codex on another you make the worktrees yourself or use a tool that runs both.
Tools that do all of this for you
Desktop apps that make a worktree per agent and track the branches. Facts are from each tool's own docs as of 5 December 2026.
- Conductor (macOS, plus cloud workspaces on paid plans; now "Conductor from Meta"). Makes a workspace per task under
~/conductor/workspaces/<repo>/<name>, each on its own branch off the base branch. It copies gitignored files matching.worktreeinclude, else its Files to copy setting, else.env*. Setup and archive scripts are saved in each person's Conductor settings, not the repo, and reach the main checkout through$CONDUCTOR_ROOT_PATH(cp "$CONDUCTOR_ROOT_PATH/.env" .env). Each workspace gets 10 ports starting atCONDUCTOR_PORT. Archiving saves the git state, uncommitted files included. It runs Claude Code, Codex, Cursor, OpenCode, Muse Code and Antigravity. The app is free today. - Pane (macOS, Windows, Linux; free, AGPL-3.0). Makes a worktree and branch per pane, under
<repo>/worktrees/by default. It copies.env*, agent folders andnode_modulesby default and runs setup, run and archive scripts from its project settings, elsepane.json(or aconductor.json,.gitpod.ymlor devcontainer file you already have). Each pane gets 10 ports starting atPANE_PORT. Note that archiving a pane from the app removes the worktree, uncommitted changes included. The branch stays. - Trek (macOS, Windows, Linux preview; free, Apache-2.0; I make it). Choosing New worktree on a thread makes a worktree on a
trek/branch off your current branch, in Trek's own data folder, and copies.envand.env.localif git ignores them (the list is editable per project). It runs Claude Code, Codex, OpenCode, Cursor, Copilot and other agents side by side in one window, plus models from an API key or a local one such as Ollama. It merges a branch only when your main checkout is on the base branch with no uncommitted changes, undoes a merge that conflicts, and never deletes a worktree on its own. It doesn't run a setup script or assign ports itself. Use the hook above, or a project Action that runs in the thread's worktree.
In the terminal, Worktrunk wraps making, switching and merging worktrees in one CLI, with hooks for setup.
Which one?
- Claude Code only:
claude -wand a.worktreeincludefile. - A setup script and ports for every worktree: Conductor on a Mac, Pane anywhere.
- Codex only: the Codex app's Worktree option, which reads
.worktreeincludetoo. - Mac, Windows and Linux under Apache-2.0, with API-key or local models next to the CLI agents, and an app that never deletes a worktree on its own: Trek.
- Two agents, now, nothing to install: section 1 and a hook.
Checklist
- [ ] A new branch per agent
- [ ]
.envfiles copied (by hand, hook,.worktreeincludeor your app) - [ ] Dependencies installed in each worktree
- [ ] A different port per dev server
- [ ] Shared things (database, Docker) separated, or left alone
- [ ] Each branch reviewed before you merge it
- [ ]
git worktree removeonce it's merged
Sources
- git worktree: https://git-scm.com/docs/git-worktree
- githooks, post-checkout: https://git-scm.com/docs/githooks#_post_checkout
- Claude Code worktrees: https://code.claude.com/docs/en/worktrees
- Codex git worktrees: https://learn.chatgpt.com/docs/environments/git-worktrees
- Codex local environments: https://learn.chatgpt.com/docs/environments/local-environment
- Codex command line: https://learn.chatgpt.com/docs/developer-commands
- Conductor, files to copy: https://www.conductor.build/docs/reference/files-to-copy
- Conductor, scripts: https://www.conductor.build/docs/reference/scripts
- Pane docs: https://runpane.com/docs/panes-and-worktrees and https://runpane.com/docs/configuration
- Worktrunk: https://worktrunk.dev/
- Trek's worktrees, with a longer comparison: https://trekdesktop.com/worktrees/


