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:
# Agent 1 starts workingclaude-agent "Implement the login feature"# Creates changes in working directory
# Agent 2 tries to work in parallelclaude-agent "Write tests for login feature"# ERROR: Working directory has uncommitted changes# Agent 2 can't checkout test branchThe 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.
# 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
# From your main repositorycd /path/to/my-repo
# Create worktree for feature agentgit worktree add ../agent-feature feat/new-feature
# Create worktree for test agentgit worktree add ../agent-tests test/new-feature-tests
# Create worktree for review agentgit worktree add ../agent-review review/current-pr
# List all worktreesgit worktree listThe output shows all worktrees:
# 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:
# Make changes in feature worktreecd ../agent-featureecho "new code" > feature.pygit status# Shows: modified: feature.py (on feat/new-feature)
# Check main worktreecd ../my-repogit status# Shows: nothing to commit, working tree clean (on main)
# Check test worktreecd ../agent-testsgit 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:
- Branch isolation: Each agent works on its own branch
- File isolation: Agents don’t overwrite each other’s changes
- Shared history: Agents can see all commits from other agents
- 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 1: Feature agentcd /path/to/agent-featureclaude-agent "Implement the login feature with OAuth2 support"
# Terminal 2: Test agent (in parallel)cd /path/to/agent-testsclaude-agent "Write unit tests for the login feature"
# Terminal 3: Review agent (in parallel)cd /path/to/agent-reviewclaude-agent "Review the code in feat/new-feature branch"All three agents run simultaneously. Each has its own working directory and branch.
# Timeline of parallel executionTime 0:00 - Agent 1 starts implementing loginTime 0:00 - Agent 2 starts writing tests (parallel)Time 0:00 - Agent 3 starts reviewing (parallel)Time 0:30 - Agent 1 commits feature changesTime 0:35 - Agent 3 sees Agent 1's commits (shared history)Time 0:40 - Agent 2 commits test changesTime 0:45 - All agents complete their tasksAgents 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:
- Developer Agent: Implements the feature
- Tester Agent: Writes tests
- Reviewer Agent: Reviews code quality
Directory Structure
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
# Create branches firstgit branch feat/logingit branch test/logingit branch review/login-pr
# Create worktreesgit worktree add ../worktrees/dev-login feat/logingit worktree add ../worktrees/test-login test/logingit worktree add ../worktrees/review-login review/login-pr
# Verifygit worktree listRunning Agents
# Start developer agentcd ../worktrees/dev-loginclaude-agent "Implement login with JWT authentication. Create:- Login endpoint at /api/auth/login- User model with password hashing- Session managementCommit when complete."
# In parallel, start tester agentcd ../worktrees/test-login &claude-agent "Write tests for login feature:- Unit tests for password hashing- Integration tests for login endpoint- Test cases for session managementUse pytest. Commit when complete."
# In parallel, start reviewer agentcd ../worktrees/review-login &claude-agent "Review code quality:- Check feat/login branch- Look for security issues- Suggest improvementsCreate review.md with findings."Coordinating Results
When agents finish, I merge their work:
# Check what each agent didcd ../worktrees/dev-logingit log --oneline -3# Shows: "feat: implement JWT login", "feat: add password hashing"
cd ../worktrees/test-logingit log --oneline -3# Shows: "test: add login unit tests", "test: add integration tests"
# Merge feature and tests to maincd ../my-repogit merge feat/logingit merge test/login
# Cleanup worktreesgit worktree remove ../worktrees/dev-logingit worktree remove ../worktrees/test-logingit worktree remove ../worktrees/review-loginBenefits for AI Agent Teams
Git worktree accelerates the development pipeline.
Pipeline Parallelization
Traditional sequential workflow:
# Sequential: One agent at a timeTime 0-30: Agent implements featureTime 30-60: Agent writes testsTime 60-90: Agent reviews codeTotal: 90 minutesParallel workflow with worktree:
# Parallel: All agents at onceTime 0-30: Agent implements feature (parallel)Time 0-30: Agent writes tests (parallel)Time 0-30: Agent reviews code (parallel)Total: 30 minutes3x 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 new branch and worktree togethergit worktree add -b feat/new-feature ../agent-workspace
# Create worktree from existing branchgit worktree add ../agent-workspace feat/new-feature
# Create worktree at specific commitgit worktree add ../agent-workspace abc123Removing Worktrees
# Remove worktree (must be clean first)cd ../agent-workspacegit status # Should be cleancd ../my-repogit worktree remove ../agent-workspace
# Force remove if dirtygit worktree remove --force ../agent-workspace
# Prune stale worktrees (after manual deletion)git worktree pruneChecking Worktree Status
# List all worktreesgit worktree list
# Check each worktree's statusfor wt in $(git worktree list | awk '{print $1}'); do echo "Worktree: $wt" cd $wt && git status --shortdoneLimitations
I found some things to watch for:
- Branch conflicts: Two worktrees can’t point to the same branch
- Memory usage: Each worktree uses disk space
- Cleanup needed: Remove worktrees after agents finish
- Shared config:
.gitignoreand config affect all worktrees
# ERROR: Can't have two worktrees on same branchgit worktree add ../wt1 feat/login # OKgit worktree add ../wt2 feat/login # ERROR: branch already checked outSummary
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