aq.dev / guides / git-worktrees-for-ai-coding-agents

Git Worktrees for AI Coding Agents: The Isolation Standard

A git worktree is an additional working directory attached to the same repository: one repo, many checkouts, each on its own branch. For AI coding agents, worktrees have become the standard isolation mechanism because they give every agent a private copy of the files without the cost of a second clone.

This guide explains what worktrees are, why agents need them, the exact commands to manage them by hand, the gotchas that bite in practice, and how AQ automates the whole lifecycle.

What is a git worktree?

By default a git repository has exactly one working directory. git worktree add creates more: extra directories that share the same object database and refs but hold independent checkouts. Edits, builds, and test runs in one worktree never touch the files in another.

Two rules define the model:

Compared to cloning the repo again, a worktree is faster to create, shares disk for git objects, and keeps everything under one remote configuration.

Why are worktrees the standard isolation for coding agents?

An AI coding agent is an aggressive concurrent writer. It edits many files per minute, runs formatters and test suites, and sometimes stages or commits. Put two agents (or one agent and one human) in the same checkout and they overwrite each other's edits, race on the index, and produce test failures neither caused.

Worktrees solve exactly this class of problem:

Containers and full clones also provide isolation, but worktrees hit the sweet spot for agents: strong enough to prevent interference, light enough to create one per task without thinking about it.

The manual commands

Create a worktree with a new branch, work in it, and remove it when done:

# From your main checkout: new directory, new branch
git worktree add ../myapp-fix-auth -b fix/auth-timeout

# Or base it explicitly on the latest remote main
git fetch origin
git worktree add ../myapp-fix-auth -b fix/auth-timeout origin/main

# See every worktree and its branch
git worktree list

# When the work is merged: remove the directory, then the branch
git worktree remove ../myapp-fix-auth
git branch -d fix/auth-timeout

# Clean up records of worktrees whose directories were deleted by hand
git worktree prune

That is the whole API surface you need day to day: add, list, remove, prune.

What are the gotchas with git worktrees?

How AQ automates the worktree lifecycle

AQ is the multiplayer workspace where engineering teams run AI coding agents like Claude Code and Codex together: shared live terminals, a code editor, and app previews, in your own cloud. Under the hood, an AQ workspace is exactly the setup described above, automated end to end:

Agents run as real CLIs (Claude Code, Codex, Cursor Agent, Kimi, Grok, or a plain shell) inside tmux in that worktree, on your own VM or an AQ-managed one, streamed to the browser. The agent commits, pushes, and opens PRs on the workspace branch with the user's own GitHub credentials; the worktree mechanics are invisible.

When should you manage worktrees by hand?

If you are one engineer running one or two agents, manual worktrees are genuinely fine: learn add, remove, and prune, script your dependency install, and you have a solid setup. Automation earns its keep when the numbers grow: many tasks per day, several agents in flight, or a team that needs to see and share each other's agent sessions. At that point the worktree ceremony (create, install, copy env, assign ports, tear down, prune) is a measurable tax, and a platform that treats worktree-per-task as a first-class primitive pays for itself in the setup time alone.

Frequently asked questions

Is a git worktree better than a second clone for AI agents?

Usually, yes. A worktree creates in about a second, shares the object database (so it costs far less disk and no extra fetch configuration), and sees the same refs as your main checkout, which makes rebasing onto the latest main trivial. A separate clone only wins when you want fully independent refs, for example to protect against force-pushes or ref deletion affecting other checkouts.

Do git worktrees share node_modules?

No. Dependencies live in the working directory, so each worktree needs its own install. That install is typically the slowest part of worktree setup. AQ mitigates this by installing dependencies automatically when a workspace is prepared and restoring them from a content-addressed cache when the lockfile matches.

Can two worktrees check out the same branch?

No. Git enforces one branch per worktree and refuses the checkout with an error. This is the property that makes worktrees safe for agents: each agent's branch has exactly one working directory, so there is never ambiguity about where a branch's uncommitted changes live.

How do I clean up a worktree I deleted manually?

Run git worktree prune from the main checkout. Deleting a worktree directory with rm leaves a stale administrative record that blocks reusing the path; prune clears it. The clean order is git worktree remove first, then delete the branch, and make sure no dev servers or watchers are still running from inside the directory.

Where does AQ put its worktrees?

AQ creates each workspace's worktree inside the repo's .worktrees/ directory, on a branch named ai/{id}-{slug}. Dependencies are installed automatically, the branch can be rebased onto the latest main from the workspace, and teardown kills leftover processes before removing the worktree.