Loading WURK...
RomanMotion
RomanMotion
Product ad & launch videos · 3D product visuals · app & AI builder. Concept to final cut in days. Open for projects.

Run two coding agents at once with git worktrees

Two coding agents, one repo, no file collisions: git worktrees give each agent its own folder and branch, so you review two clean diffs.

Published on November 1, 20265 min read

You want one agent fixing a login bug while another builds a new feature, so you point both at the same folder. An hour later they're overwriting each other's files, git status shows a blended diff of two unrelated tasks, and every build breaks for reasons neither agent caused. Git worktrees fix that: each agent gets its own folder and branch off the same repo, so both finish clean and you merge two branches instead of refereeing one messy directory.

The whole fix is one command you already own. git worktree ships with git itself, no new tools or subscriptions. It creates an extra working directory attached to your existing repository, checks out a different branch there, and keeps the two agents completely out of each other's files until you're ready to merge.

Two agents, one folder is the bug

Two agents in one working directory share everything: the same checked-out branch, the same staging area, the same uncommitted changes. Agent A's half-finished refactor looks like a bug to agent B, so agent B "fixes" it. You can't review one task without reviewing the other, and if both run test suites, they fail each other mid-run.

The usual workaround, "run them at different times", throws away the whole point of having agents. The parallelism isn't the risk. The shared folder is.

What a worktree actually is

A worktree is an additional working directory attached to the same repository, each with its own checked-out branch, while they all share the underlying repo history (git-scm.com/docs/git-worktree). Your main folder keeps whatever branch it's on; the new folder gets its own. Nothing is copied or forked: both folders see the same commits, the same remotes, the same branches.

Two details matter for agent work. First, a branch can only be checked out in one worktree at a time, so give each agent its own branch name. Second, untracked and ignored files like .env or node_modules belong to one folder, not the repo, so each worktree needs its own install and its own env file. That's not overhead, it's isolation working as intended: agent B can't read agent A's half-written config because it doesn't exist in B's folder.

Setting up two agents in parallel

From your main checkout, create a second folder with a new branch:

git worktree add ../my-app-bugfix -b fix/login

Now point agent A at ../my-app-bugfix with a prompt like "fix the login redirect, commit to fix/login", and leave agent B in the original folder on its feature branch. They can work at the same time without ever touching the same files.

Then handle the runtime, because both agents may want to build and test:

  • Install dependencies in the new worktree with your usual package manager. node_modules doesn't carry over.
  • Copy or recreate .env there, with its own values if needed.
  • If both run dev servers, give each its own port, for example PORT=3001 for one and PORT=3002 for the other.
  • If both hit a database, point them at separate local databases. Two agents writing test rows into one dev database is its own kind of collision.

Check what you've got any time with git worktree list. It shows every worktree, its path, and its checked-out branch.

Any coding agent that can be pointed at a folder works this way. If you're still choosing one, you can try the zcode agent harness free (our invite link; the app is free, though you still bring a model plan or API key, per the ZCode FAQ).

Review, merge, then clean up

Each agent's work lands on its own branch, which means your normal review flow just works. Read the diff for fix/login on its own, free of feature noise, and likewise for the feature branch. Merge them into main one at a time, through a pull request or from a folder that has main checked out and no agent working in it, and fix any real conflict once, in your own editor, not through an agent guessing.

When a branch is merged and its worktree is no longer needed:

git worktree remove ../my-app-bugfix
git worktree prune

remove deletes the folder, and it refuses to do so if the worktree has uncommitted changes, which is a useful safety net. prune clears leftover entries for worktrees you deleted by hand. Delete the merged branch with git branch -d as usual.

When not to parallelise

Two worktrees prevent file collisions. They do nothing for work that collides by meaning. Don't split tasks this way when:

  • Both tasks touch the same files. Two agents redesigning the same component will produce conflicting branches no matter what folders they sit in.
  • Both need database migrations. Two conflicting schema changes are a human problem to sequence, not a folder problem.
  • One task's output is the other's input, like "add the API endpoint" and "build the UI for it". Chain those instead, or run them on one branch.

Slice work along file and module boundaries: agent A owns auth, agent B owns the dashboard. When the boundary is real, worktrees are enough. When it isn't, no folder trick will save you. Our guide to breaking a feature into agent-sized tasks covers how to draw those lines.

Two agents running at once also means more agent usage. If you cover that with a subscription rather than a per-request meter, our referral link gives a referral discount on GLM coding plans on a first subscription; Z.ai's campaign terms set the conditions, and we may receive referral credits when an invitation qualifies.

Copy-paste cheat sheet

# See your worktrees
git worktree list

# New folder + new branch for agent B
git worktree add ../my-app-bugfix -b fix/login
cd ../my-app-bugfix
npm install          # deps per worktree
cp ../my-app/.env .  # env per worktree
PORT=3002 npm run dev

# Agent A keeps working in the original folder on its own branch

# After review, in a folder with main checked out (no agent in it):
git switch main
git merge fix/login
git worktree remove ../my-app-bugfix
git worktree prune
git branch -d fix/login

We keep more notes on working this way: more on our blog.

If this saved you an untangling session, a small tip on the post is always appreciated.

Conversation

Join the conversation

Share your thoughts with the author and other readers.

Comments0

Checking your account…

Latest comments

0
No comments yet. Be the first to share your thoughts.