No history yet

Agentic Worktree Infrastructure

Building a Multi-Agent Workspace

You already know that Git worktrees let you check out multiple branches at once. Now, let's adapt that concept for AI agents. When you run multiple agents like Claude Code on the same project, they need their own sandboxes. Without proper separation, they can overwrite each other's work, get confused by conflicting file states, and pollute each other's context. An organized worktree structure prevents this chaos.

The goal is to create a clean, isolated environment for each agent, allowing them to work in parallel on different tasks, just like a human development team.

An Optimal Folder Structure

The first step is to avoid cluttering your project's root directory. A common practice is to create a dedicated, untracked directory to house all your worktrees. A name like .trees/ is a good convention because the leading dot often hides it from standard directory listings, and its name clearly states its purpose. You should also add this directory to your project's .gitignore file to ensure it's never committed to the repository.

# In your project's root directory
mkdir .trees

# Add it to your .gitignore
echo ".trees/" >> .gitignore

Now, when you create a worktree, you'll place it inside this subdirectory. This keeps your main working directory clean and makes it easy to see all active agent workspaces at a glance.

This structure keeps the main branch pristine while the agents experiment in their own isolated folders.

Managing Dependencies and State

A critical feature of worktrees is that they don't share Git-ignored files. This means each worktree maintains its own node_modules directory, .env files, build artifacts, and agent-specific state or memory files. This isolation is exactly what we need for running agents in parallel.

Imagine one agent is tasked with upgrading a dependency to a new major version, while another is fixing a bug in the current version. If they shared a node_modules directory, their tasks would be impossible. Each worktree must have its own set of dependencies to function independently. When you create a new worktree, you will need to run your dependency installation command (like npm install or pip install -r requirements.txt) inside that worktree's directory.

This setup is perfect for agents. Claude can read and write files, install packages, and run tests within its assigned worktree without any risk of interfering with another agent's session.

Role-Based Branches

To keep things organized, it's best to name branches based on the agent's role or task. This makes it clear what each agent is supposed to be doing. For example, you might use prefixes like agent/ to group all agent-related branches.

Good branch names could be agent/feature-implementation, agent/bug-fixing, or agent/documentation-generation.

When creating a new worktree for a new task, you'll want to create a new branch for it at the same time. The git worktree add command has flags to help you standardize this process. The -b flag creates a new branch.

# Create a new worktree AND a new branch for a feature agent
# The new branch 'agent/feature-impl' starts from the 'main' branch
git worktree add -b agent/feature-impl .trees/feature-agent main

This single command creates the agent/feature-impl branch based on main, checks it out into the .trees/feature-agent directory, and links it to your main repository. The agent now has a fresh, dedicated workspace.

By combining a clean folder structure with role-based branches and isolated dependencies, you create a robust infrastructure for orchestrating multiple AI agents. Each agent can operate autonomously in its own workspace, minimizing conflicts and maximizing parallel productivity.

Quiz Questions 1/4

Why is it beneficial to use Git worktrees when running multiple AI agents on the same project?

Quiz Questions 2/4

What is a recommended best practice for organizing the directories for your AI agent worktrees?

This structured approach sets the stage for more complex, multi-agent workflows.