Git Worktrees for AI Agents: Parallel Work Without Shared Files
Git worktrees make parallel AI coding practical when each agent owns a separate task directory and branch. The reliable workflow also assigns runtime resources, defines dependencies, and reserves integration for one reviewer. This guide walks through that complete operating model.
AI-assisted technical guide with linked primary sources. Examples are illustrative unless stated otherwise. How these guides are made.
Give every agent a branch and a directory
Two agents editing one checkout create an attribution problem: a passing test may depend on another agent’s unfinished changes, and a commit may accidentally include both tasks. Git worktrees provide separate working directories attached to one repository. Each checkout can hold a different branch; the repository still shares objects and much repository metadata. This is documented in the Git worktree manual.
Use that capability as a workflow boundary: one task, one branch, one directory, one owner. This article’s operating model is a suggested team convention. It does not make an agent trustworthy or prevent it from touching files elsewhere on your machine. A worktree separates ordinary editing activity; a sandbox restricts capabilities. Use both when the task requires both.
Choose tasks by integration boundary. A parser fix and its documentation can be independent if their interface is already stable. Two redesigns of the same parser usually need an agreed contract first. Directory separation reduces accidental interference but cannot reconcile incompatible product decisions.
Create both worktrees from the same known base
Here is an illustrative setup for a repository whose default branch is main. Run it from the existing repository after checking your working state. Replace names for your project; these commands are a recipe rather than a claim that this environment has run them.
git status --short --branch
git fetch origin
git worktree add -b agent/parser-fix ../app-parser origin/main
git worktree add -b agent/parser-docs ../app-parser-docs origin/main
git worktree listBefore launching agents, record the starting commit in each task brief. Give the parser agent permission to change implementation and targeted tests; give the documentation agent an explicit assumption about the parser interface. If that assumption changes, pause dependent writing instead of letting the second agent guess.
Do not put the worktrees inside generated directories or rely on two nearly identical tab labels. A practical naming convention is project-task for the directory and agent/task for the branch. Keep customer names, credentials, and private ticket text out of labels that may appear in screenshots.
Separate runtime resources as well as files
File isolation leaves other collisions unresolved. Two development servers may request the same port. Two tests may mutate the same database. Two agents may rewrite a shared fixture or deploy into the same preview environment. Prepare an allocation sheet before concurrency begins.
| Resource | Parser task | Documentation task |
|---|---|---|
| Branch | agent/parser-fix | agent/parser-docs |
| Working directory | app-parser | app-parser-docs |
| Local server port | 4101 | 4102 |
| Write scope | Parser and regression tests | Documentation and examples |
| Integration authority | One named reviewer | |
Install dependencies using the repository’s existing lockfile and package manager. Avoid sharing a writable dependency directory between tasks just to save setup time. Decide which caches are safe to share and which generated outputs are task specific. For database integration tests, use separate test databases or serialize those tests.
Assign lockfile changes to one task if both agents need dependencies. The same rule applies to database migrations and generated client code: name an owner or introduce a prerequisite task.
Review each task as a reproducible deliverable
An agent’s final message should identify its branch, commit, changed behavior, verification command, result, and remaining uncertainty. Ask for the exact command and relevant failure output rather than “tests passed.” Record environmental blockers separately from product failures.
Git status reports staged, unstaged, and untracked changes; its porcelain format is intended for stable machine parsing. Include a status inspection in the handoff so a clean-looking summary cannot hide an uncommitted fixture.
git -C ../app-parser status --short --branch
git -C ../app-parser log -3 --oneline
git -C ../app-parser diff origin/main...HEADRead the regression test before trusting it. A test that duplicates the new implementation may pass while preserving the original misunderstanding. For the parser example, request one input that previously failed and one boundary case that should remain rejected. Verify the documented behavior against those examples.
Integrate through one controlled review lane
Use the project’s existing pull request process where available. For local integration, choose either merging the complete task branch or selecting specific reviewed commits. Cherry-pick applies changes from existing commits and records new commits; it expects a clean working tree. It is useful when only part of a task branch belongs in the delivery, but selected commits must include their prerequisites.
For a complete reviewed branch, an illustrative integration sequence is:
git switch main
git status --short
git merge --no-ff agent/parser-fix
# Run the project’s documented parser and integration checks.
git merge --no-ff agent/parser-docs
# Build documentation and verify examples against the combined code.Git merge combines branch histories and can stop on conflicts. A clean textual merge is not evidence of compatible behavior. The documentation may still describe an old error format even when no file conflicts appear. Review the combined result and run checks after integration.
Do not let several agents independently update the integration branch. A single owner makes sequencing, rollback decisions, and the final completion claim understandable.
Handle blocked branches and dependencies explicitly
If the documentation task needs an interface change that has not landed, mark it waiting with the precise dependency. Let it prepare unaffected material, but hold its final examples. If both tasks discover a shared design problem, stop expanding their scope and create a small prerequisite decision.
If an agent commits unrelated changes, isolate the intended change through review before integration. If a test fails only after merging, preserve the individual task evidence and investigate the interaction. Running more parallel sessions will not resolve an ambiguous contract.
When two agents need the same branch, create separate task branches instead. Git ordinarily protects against checking one branch out in multiple worktrees; overriding that safeguard should not be the default workflow. For a long experiment with uncertain ownership, a separate clone may make the repository boundary easier to understand.
Close the task without losing useful work
Before removing a worktree, confirm the changes are integrated or deliberately preserved. Inspect untracked outputs and stop its development servers. Then remove the linked working directory through Git rather than deleting it by habit:
git worktree remove ../app-parser
git worktree remove ../app-parser-docs
git worktree listUse this completion checklist:
- The final branch contains the intended behavior and its verification evidence.
- Dependent examples match the integrated interface.
- Useful untracked work has been saved intentionally.
- Task servers, database fixtures, and temporary credentials are closed or revoked.
- The handoff records the resulting commit and remaining follow-up work.
Keep task branches until your team’s normal retention policy allows deletion. Finishing the cleanup is part of the task; otherwise an abandoned server or stale directory becomes the next session’s misleading starting point.
Keep the right agent session in sight
Once each task has an identifiable directory and branch, session visibility becomes more useful. Purrvio’s Mac preview shows supported local coding sessions so you can notice work that needs attention and return to the relevant terminal. Claude Code and Codex have the richest session support.
Purrvio observes local session records and processes without installing agent hooks. It does not create worktrees, enforce write boundaries, or merge branches. Treat it as a companion to the ownership model described here. Navigation supports Ghostty and Terminal.app; direct replies require Ghostty and Automation permission.
The free preview supports macOS 13 or later on Apple silicon and Intel Macs. It is locally signed and is not Apple-notarized. Download Purrvio and read the first-launch notes when you want local agent visibility alongside your worktree workflow.
Frequently asked questions
Are Git worktrees a security sandbox for AI agents?
No. They separate working directories and branch state for ordinary development. An agent may still access other permitted files, processes, and network services. Use actual permission controls or sandboxing for security boundaries.
Should every agent have its own worktree?
Give independent implementation tasks separate worktrees when they run concurrently. Read-only reviewers may not need one, while tightly dependent tasks may be simpler to run sequentially until their interface is stable.
Why do tests pass in each worktree but fail after integration?
Individual branches can satisfy different assumptions. Combined code may expose interface, fixture, migration, or dependency interactions. Review the integrated behavior and run relevant checks again after merging.
Sources and further reading
Primary references checked for this guide on 10 November 2026. Documentation can change; confirm version-specific behavior before using commands in a production workflow.
- Git worktree manual
Verified linked worktree creation, shared repository structure, branch protections, and removal.
- Git status manual
Verified staged, unstaged, untracked status and stable porcelain output.
- Git cherry-pick manual
Verified applying existing commits, recording new commits, and clean working tree requirement.
- Git merge manual
Verified branch integration and conflict behavior; semantic review recommendations are the article’s operating guidance.