Git Worktree Workflow for Concurrent Feature Development
Git worktrees let multiple AI agents edit code simultaneously without corrupting each other's work.

Two AI coding agents working in the same directory at the same time do not produce slower progress. They produce corruption. That distinction matters because it changes how engineering teams ought to think about the risk: this is not a performance tax to be tolerated, but a correctness problem that appears as broken builds and lost work.
The failure modes are specific and repeatable. Agents overwrite each other's in-progress edits before either finishes. Concurrent git add operations from separate sessions corrupt the staging area. If an agent reads a file mid-edit by another agent, it reasons from a state that never actually existed, so it builds code on a false premise. And an agent's internal model of the codebase, built up over a conversation, drifts away from whatever is actually sitting on disk. None of these are rare accidents that only occur under unusual load. They are the default outcome of pointing two autonomous sessions at the same tree, because most AI coding agents are built on the assumption that they have exclusive access to the project directory.
Human developers rarely hit this wall because they are naturally slow enough to avoid it. A person typing code is a bottleneck by nature: one file open, one set of changes in flight, one train of thought at a time. Agents carry no such limit. Point several at a repository with no coordination layer: they will execute at full speed simultaneously, each unaware that another is rewriting the same lines. One technical guide on multi-agent coding workflows puts the consequence bluntly: strip out the coordination, and five agents racing to edit the same lines run slower than a single agent working alone. Parallelism without isolation does not multiply output. It cancels it.
The obvious fix, giving each agent its own full clone of the repository, solves the collision issue but creates a worse one. Clones duplicate the entire object database on disk, each needs its own remote tracking configuration kept in sync by hand, and merging work back across several independent clones turns into the kind of rebase nightmare that erases whatever time parallelism was supposed to save. You need a mechanism that gives each agent a private file system without duplicating the repository's history. That mechanism already exists in git, and most teams running parallel agents today have simply not adopted it yet.
What a git worktree shares versus isolates
A git worktree is a linked working directory that shares the same object database as the main checkout, but it keeps its own HEAD, its own index, and its own working tree files separate from every other worktree attached to that repository. A git worktree's isolation stops at a specific boundary, and knowing exactly where that boundary sits is what makes worktrees usable for parallel agent work rather than a source of confusion.
When two AI coding agents operate concurrently in the same tree, the result is not slower progress but active corruption, so picture instead one main worktree with two linked worktrees branching off it. Each of the three has its own HEAD, so each can sit on a different branch at the same time. Each has its own index, so staging a file in one worktree has no effect on the staging area of another. And each has its own working tree files on disk, so an agent editing a file in one worktree cannot touch the file an agent is editing in another. What they share is everything that makes them part of the same repository: the object database holding every commit, blob, and tree. A commit made in one worktree is visible to every other worktree attached to that repo the moment it lands, because all worktrees share one single history.
That shared history is also where the hard constraint comes from. Because branches live in the shared refs rather than inside any individual worktree, a given branch can only be checked out in one worktree at a time. So every parallel agent needs its own distinct branch, not just its own directory. Structurally, the main worktree is the only one with an actual .git folder on disk; every linked worktree carries a .git file instead, a short pointer back to the main repository's worktree-specific metadata. Agents working this way get complete isolation at the file level. They can see each other's committed history through the shared object store, which is often useful, but they cannot touch each other's uncommitted work, which is the property that prevents corruption.
The core commands for creating, listing, and removing worktrees
Four commands cover the entire lifecycle of a worktree, from creation through inspection to cleanup, and the options on each one affect whether a worktree can be created, found, or cleaned up correctly.
To create a worktree on a brand-new branch, run:
git worktree add -b feature-auth ../project-auth main
This checks out a new branch called feature-auth, based on main, into a new directory at ../project-auth. The -b flag is only needed when the branch doesn't already exist. To attach a worktree to a branch that's already there, drop the flag:
git worktree add ../project-bugfix bugfix-123
To see every worktree currently attached to the repository, along with the branch and commit each one is sitting on:
git worktree list
Once an agent's task is finished and merged, remove its worktree directory with:
git worktree remove ../project-auth
Worktrees created and destroyed frequently, which is the normal rhythm of agent-driven development, can leave behind stale metadata when a directory is deleted by hand instead of through git worktree remove. Running git worktree prune clears that metadata out, and it's worth running regularly in any workflow where worktrees come and go often.
Because that directory sits inside the project tree, it needs to be added to .gitignore, or the contents of every worktree appear as untracked files in the main checkout.
Four coordination patterns for parallel agent work
Creating worktrees isolates each agent's files. It doesn't by itself tell a team how to divide work across them, and the choice of coordination pattern depends on what kind of task is being split.
The most common pattern is worktree-per-task. Each agent gets its own directory, for instance project-feature-auth/ for one agent, project-bugfix-422/ for a second, and project-perf-optimize/ for a third. This pattern works cleanly when the tasks are genuinely independent and touch different modules of the codebase. It does nothing to help when two tasks need to touch the same files, and the step that makes it safe, deciding in advance which files and interfaces belong to which agent, is also the step teams most often skip. Without that spec-driven decomposition laid out before any agent starts, two agents will independently rewrite the same function, and the worktree isolation that was supposed to prevent conflict just delays the collision to merge time. A practical setup script for this pattern generates a timestamped branch name, creates the worktree off main, and runs the project's install step inside the new directory before handing it to an agent. The incident response platform incident.io runs four to five parallel Claude Code agents this way as routine practice, using a bash function that takes a project name and a task name and spins up an isolated Claude session on a new branch in one step.
Pattern 2, pipeline staging, with sequential agents sharing state through commits, applies when tasks are not independent but sequential, each depending on the output of the one before it. An analysis agent commits a spec to a branch; an implementation agent checks out that branch, reads the spec, and commits code against it; a review agent commits its notes on top; then a test agent writes tests against the final state. Each stage runs in the same worktree, checking out the prior stage's branch before starting its own work, which keeps the handoff simple and avoids merge complexity. Because every stage is a commit, the chain of commits becomes an audit trail of what each agent decided and why, something you don't get by default with worktree-per-task.
The coordinator-specialist-verifier pattern applies to tasks where a mistake is more costly than extra time spent catching it. A coordinator agent breaks the task into pieces, specialist agents execute those pieces in parallel worktrees, and a separate verifier agent checks the combined result against the original spec. Tabnine's CTO Eran Yahav described this shape to InfoWorld this way: one agent writes code, another tests it, a third handles documentation or validation, and a fourth checks for security and compliance. The logic behind the separation is the same logic behind separating author and reviewer on a human team: an agent whose only job is to find fault in someone else's work is far more likely to catch a real bug than the agent that wrote the code is to catch its own mistake. The same reasoning extends to debugging specifically. A single agent chasing down a bug tends to fixate on the first plausible explanation it finds, while three agents pursuing competing hypotheses in parallel are more likely to surface the alternative explanation that the first agent would have missed entirely.
The fourth pattern, fan-out, fits large mechanical jobs rather than reasoning-heavy ones, such as migrations where the same transformation needs to apply across dozens or hundreds of files. The approach loops a headless agent over a file list, one isolated invocation per file, with tool permissions scoped tightly to reading, editing, and committing. A typical invocation asks the agent to migrate a single file and return either OK or FAIL, and the sensible practice is to test the loop on two or three files before turning it loose on the full list. Each invocation stands alone and can be audited independently, and a failure in one file has no effect on the rest of the run.
None of these patterns erase the underlying limits of what agents can coordinate on their own. Anthropic's engineering write-up on its own research system is direct about this: most coding tasks involve fewer truly parallelizable pieces than research tasks do, and current agents are not yet good at coordinating and delegating to each other in real time.
Database and port isolation: completing the environment per worktree
Separating the filesystem isolates each agent's files but leaves shared infrastructure underneath untouched. Shared infrastructure underneath the filesystem, namely the database and the dev server port, is where state still bleeds between agents even after worktrees are in place.
The failure here is quieter than a file collision, and harder to trace, which makes it more dangerous in practice. When two Claude Code sessions point at a single local database, a migration or a seed script run by one agent changes the schema or the dataset the other agent is actively testing against. The second agent's tests start failing for reasons that have nothing to do with the feature it's building, and tracking that down costs real debugging time before anyone realizes the database was never isolated in the first place.
# my-project-feature-a/.env.local
DATABASE_URL=./data/feature-a.sqlite
createdb my-project-feature-a
createdb my-project-feature-b
Each worktree then points at its own database through its own DATABASE_URL entry in its own .env.local file. The dev server port needs the same treatment: each worktree gets a distinct port assigned through an environment variable in that same file, so an agent can run its own dev server and its own live tests without colliding with the dev server another agent has already started. Teams running more than a handful of agents at once are converging on a standard built from these pieces: one worktree per agent, paired with one lightweight container per worktree that supplies an isolated database and dev server for that agent alone. Worktrees isolate the files; the .env.local pattern is what extends that same isolation down into the rest of the running environment.
Claude Code's native worktree support and the subagent isolation directive
Everything described above can be set up by hand, with scripts that create the worktree, install dependencies, and write the .env.local file. Claude Code now does a version of this automatically. Its --worktree flag creates an isolated workspace and starts a Claude session inside it in a single step:
claude --worktree feature-auth
Running several sessions in parallel is just a matter of starting more than one:
claude --worktree feature-a & claude --worktree bugfix-123 &
Each worktree created this way lives at <repo>/.claude/worktrees/<name>, branches off the repository's default remote branch, and gets a branch named worktree-<name>. Cleanup is handled automatically too: if a session exits having made no changes, Claude removes the worktree and its branch on its own; if changes or commits exist, Claude prompts to keep or discard them rather than deleting work silently.
This removes most of the manual lifecycle management, creation, branch naming, cleanup, that made running several agents in parallel feel fragile when done entirely by hand. The coordination patterns described earlier, worktree-per-task, pipeline staging, coordinator-specialist-verifier, and fan-out, still apply exactly as before. What changes is how much scaffolding a team has to build before it can use them. The isolation that once needed a bash script and a disciplined habit of running git worktree prune is becoming a flag on the command line, and the patterns that depend on it are becoming correspondingly easier to adopt at the scale parallel agent work actually demands.


