Run coding agents in parallel with git worktrees
Two agents in one folder edit the same files. One git command gives each agent a worktree of its own.
The commands
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
For the next agent, open another terminal and do the same with another name. This works with any agent, since each one only sees a folder.
These list your worktrees and remove one once its branch is merged.
git worktree list
git worktree remove ../myapp-login
Claude Code can make the worktree for you. claude --worktree fix-login starts it in .claude/worktrees/fix-login, on a new branch named worktree-fix-login. The repository needs at least one commit first.
What a worktree is
A worktree is a second checkout of the same repository. It has its own folder and its own branch. All of them share one .git. A commit made in one can be merged from any other right away.
Put each agent in its own worktree. Then one agent’s half-done edits never break the other’s tests.
The catches
- Git won’t check out one branch in two worktrees. Give each agent a new branch.
- A new worktree has no ignored files. Copy your
.envfiles in, or most apps won’t start. - Dependencies live in each folder. Run your install step in every new worktree.
- Two dev servers want the same port. Give each worktree its own.
- Anything outside the folder is still shared. A local database is the usual one.
- Merging is still your job. Bring each branch back one at a time, or open a pull request for each.
With Trek
Trek is a free desktop app that runs your coding agents side by side. On a new thread in a Git project, turn on Worktree before you send.
Trek makes the checkout in its own data folder, on a trek/ branch off the branch you’re on. It copies .env and .env.local into it. Project Settings changes that list.
The thread then runs in that worktree, with whichever agent you picked. When it’s done, review what changed against the base branch. From the same thread you can commit, push, open a pull request or merge the branch into the base.
Removing a worktree asks first if it would lose uncommitted changes. If you also delete the branch, it asks when the branch has commits the base doesn’t.
Each worktree thread shows its own branch in the sidebar.