Skip to content

Git Worktree for AI Agents: Enabling Parallel Development Workflows

Purpose

This post shows how git worktree enables multiple AI agents to work simultaneously on the same repository without conflicts.

Problem

I wanted to run multiple AI coding agents in parallel on the same project. One agent to implement features, another to write tests, and a third to review code.

But when I tried this with a single working directory, I ran into problems:

Terminal
# Agent 1 starts working
claude-agent "Implement the login feature"
# Creates changes in working directory
# Agent 2 tries to work in parallel
claude-agent "Write tests for login feature"
# ERROR: Working directory has uncommitted changes
# Agent 2 can't checkout test branch

The agents competed for the same working directory. Each needed a different branch. But Git only allows one working directory per repository by default.

I needed a way to give each agent its own isolated workspace.

What is Git Worktree?

Git worktree lets you have multiple working directories attached to the same repository. Each worktree points to a different branch.

Think of it as having multiple “folders” that all share the same Git history. Each folder can checkout a different branch. Changes in one folder don’t affect others.

worktree-concept.sh
# Traditional Git: One working directory
/path/to/my-repo/ (main branch)
# Git Worktree: Multiple working directories
/path/to/my-repo/ (main branch)
/path/to/my-repo-feature/ (feat/new-feature branch)
/path/to/my-repo-tests/ (test/new-feature-tests branch)
/path/to/my-repo-review/ (review/current-pr branch)

All worktrees share the same .git folder. They share branches, history, and remote connections. But each has its own isolated working files.

Setup for AI Agents

I set up parallel agent workspaces using git worktree.

Step 1: Create Worktrees

Terminal
# From your main repository
cd /path/to/my-repo
# Create worktree for feature agent
git worktree add ../agent-feature feat/new-feature
# Create worktree for test agent
git worktree add ../agent-tests test/new-feature-tests
# Create worktree for review agent
git worktree add ../agent-review review/current-pr
# List all worktrees
git worktree list

The output shows all worktrees:

worktree-list-output
# Output
/path/to/my-repo abc123 [main]
/path/to/agent-feature def456 [feat/new-feature]
/path/to/agent-tests ghi789 [test/new-feature-tests]
/path/to/agent-review jkl012 [review/current-pr]

Each worktree is a separate folder with its own branch.

Step 2: Verify Isolation

I checked that changes in one worktree don’t affect others:

Terminal
# Make changes in feature worktree
cd ../agent-feature
echo "new code" > feature.py
git status
# Shows: modified: feature.py (on feat/new-feature)
# Check main worktree
cd ../my-repo
git status
# Shows: nothing to commit, working tree clean (on main)
# Check test worktree
cd ../agent-tests
git status
# Shows: nothing to commit, working tree clean (on test/new-feature-tests)

Changes stay isolated in each worktree. Agents can work independently.

How It Enables Parallel AI Agents

Git worktree solves the conflict problem by giving each agent its own workspace.

Why This Works for AI Agents

AI coding agents need isolated environments:

  1. Branch isolation: Each agent works on its own branch
  2. File isolation: Agents don’t overwrite each other’s changes
  3. Shared history: Agents can see all commits from other agents
  4. Easy cleanup: Remove worktrees when agents finish

This is a natural fit for worktree since it’s Git-native. No extra tools needed.

Parallel Agent Workflow

I ran three agents in parallel:

Terminal
# Terminal 1: Feature agent
cd /path/to/agent-feature
claude-agent "Implement the login feature with OAuth2 support"
# Terminal 2: Test agent (in parallel)
cd /path/to/agent-tests
claude-agent "Write unit tests for the login feature"
# Terminal 3: Review agent (in parallel)
cd /path/to/agent-review
claude-agent "Review the code in feat/new-feature branch"

All three agents run simultaneously. Each has its own working directory and branch.

parallel-diagram.sh
# Timeline of parallel execution
Time 0:00 - Agent 1 starts implementing login
Time 0:00 - Agent 2 starts writing tests (parallel)
Time 0:00 - Agent 3 starts reviewing (parallel)
Time 0:30 - Agent 1 commits feature changes
Time 0:35 - Agent 3 sees Agent 1's commits (shared history)
Time 0:40 - Agent 2 commits test changes
Time 0:45 - All agents complete their tasks

Agents share the Git history. When one agent commits, others can see it immediately.

Practical Setup Scenario

I set up a typical AI agent development workflow.

Scenario: Feature Development Pipeline

I have three specialized agents for a login feature:

  1. Developer Agent: Implements the feature
  2. Tester Agent: Writes tests
  3. Reviewer Agent: Reviews code quality

Directory Structure

Directory Layout
my-project/
├── .git/ # Shared Git data
├── src/ # Main worktree (main branch)
../worktrees/
├── dev-login/ # Developer agent (feat/login branch)
├── test-login/ # Tester agent (test/login branch)
└── review-login/ # Reviewer agent (review/login-pr branch)

Setup Commands

setup-worktrees.sh
# Create branches first
git branch feat/login
git branch test/login
git branch review/login-pr
# Create worktrees
git worktree add ../worktrees/dev-login feat/login
git worktree add ../worktrees/test-login test/login
git worktree add ../worktrees/review-login review/login-pr
# Verify
git worktree list

Running Agents

run-agents.sh
# Start developer agent
cd ../worktrees/dev-login
claude-agent "Implement login with JWT authentication. Create:
- Login endpoint at /api/auth/login
- User model with password hashing
- Session management
Commit when complete."
# In parallel, start tester agent
cd ../worktrees/test-login &
claude-agent "Write tests for login feature:
- Unit tests for password hashing
- Integration tests for login endpoint
- Test cases for session management
Use pytest. Commit when complete."
# In parallel, start reviewer agent
cd ../worktrees/review-login &
claude-agent "Review code quality:
- Check feat/login branch
- Look for security issues
- Suggest improvements
Create review.md with findings."

Coordinating Results

When agents finish, I merge their work:

merge-results.sh
# Check what each agent did
cd ../worktrees/dev-login
git log --oneline -3
# Shows: "feat: implement JWT login", "feat: add password hashing"
cd ../worktrees/test-login
git log --oneline -3
# Shows: "test: add login unit tests", "test: add integration tests"
# Merge feature and tests to main
cd ../my-repo
git merge feat/login
git merge test/login
# Cleanup worktrees
git worktree remove ../worktrees/dev-login
git worktree remove ../worktrees/test-login
git worktree remove ../worktrees/review-login

Benefits for AI Agent Teams

Git worktree accelerates the development pipeline.

Pipeline Parallelization

Traditional sequential workflow:

sequential-workflow.sh
# Sequential: One agent at a time
Time 0-30: Agent implements feature
Time 30-60: Agent writes tests
Time 60-90: Agent reviews code
Total: 90 minutes

Parallel workflow with worktree:

parallel-workflow.sh
# Parallel: All agents at once
Time 0-30: Agent implements feature (parallel)
Time 0-30: Agent writes tests (parallel)
Time 0-30: Agent reviews code (parallel)
Total: 30 minutes

3x faster for tasks that can run in parallel.

Digital Teams

The trend toward “digital teams” fits worktree perfectly:

  • Multiple specialized AI agents
  • Each agent has a role (developer, tester, reviewer)
  • Agents work on different branches
  • Results merge at the end

Git worktree is the infrastructure for this workflow.

Managing Worktrees

I learned some best practices for managing agent worktrees.

Creating Worktrees

create-worktree.sh
# Create new branch and worktree together
git worktree add -b feat/new-feature ../agent-workspace
# Create worktree from existing branch
git worktree add ../agent-workspace feat/new-feature
# Create worktree at specific commit
git worktree add ../agent-workspace abc123

Removing Worktrees

remove-worktree.sh
# Remove worktree (must be clean first)
cd ../agent-workspace
git status # Should be clean
cd ../my-repo
git worktree remove ../agent-workspace
# Force remove if dirty
git worktree remove --force ../agent-workspace
# Prune stale worktrees (after manual deletion)
git worktree prune

Checking Worktree Status

worktree-status.sh
# List all worktrees
git worktree list
# Check each worktree's status
for wt in $(git worktree list | awk '{print $1}'); do
echo "Worktree: $wt"
cd $wt && git status --short
done

Limitations

I found some things to watch for:

  1. Branch conflicts: Two worktrees can’t point to the same branch
  2. Memory usage: Each worktree uses disk space
  3. Cleanup needed: Remove worktrees after agents finish
  4. Shared config: .gitignore and config affect all worktrees
branch-conflict.sh
# ERROR: Can't have two worktrees on same branch
git worktree add ../wt1 feat/login # OK
git worktree add ../wt2 feat/login # ERROR: branch already checked out

Summary

In this post, I showed how git worktree enables multiple AI agents to work simultaneously on the same repository. The key point is giving each agent its own working directory and branch.

Git worktree solves the problem of agents competing for the same working directory. Each worktree provides isolation while sharing Git history. This enables parallel agent workflows that are 3x faster than sequential execution.

The setup is simple: create worktrees for each agent, run agents in parallel, then merge results and cleanup. This workflow fits the trend of “digital teams” where specialized AI agents work together on development pipelines.

Final Words + More Resources

My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

Comments