Skip to Content
Git Worktrees

Git Worktrees

AgentLoop uses Git worktrees to enable real parallel development. Each worktree is a separate working directory tied to the same repository, so multiple agents can work on different branches simultaneously without stepping on each other.

What Are Worktrees?

Git worktrees allow you to have multiple working directories from a single repository. Each worktree has its own branch and working files, but they all share the same Git history and objects.

Benefits

BenefitDescription
No file collisionsEach agent has its own isolated working directory
True parallelismMultiple agents can compile, test, and modify code simultaneously
Branch isolationChanges in one worktree don’t affect others until merged
Efficient storageWorktrees share the same Git objects, so disk overhead is small

Enabling Worktrees

Configure worktrees in your project config:

[orchestrator] use_worktrees = true worktrees_dir = ".worktrees" # Directory for worktrees (relative to project) cleanup_worktrees_on_complete = false # Auto-cleanup when tasks complete

Or set via environment variables:

export AGENTLOOP_USE_WORKTREES=true export AGENTLOOP_WORKTREES_DIR=".worktrees" export AGENTLOOP_CLEANUP_WORKTREES=false

How It Works

When the orchestrator runs with worktrees enabled:

  1. Task assignment — A task is assigned to an available agent
  2. Worktree creation — A new worktree is created with a branch for the task
  3. Agent execution — The agent works in the isolated worktree
  4. Commit — Changes are committed to the task branch
  5. Hand-off — Once the task is done, the release agent can open a PR
  6. Merge coordination — When multiple parallel branches affect overlapping code, the merge resolver agent coordinates merging
  7. Cleanup — Worktrees are optionally removed after completion

Configuration Options

OptionDefaultDescription
use_worktreesfalseEnable git worktrees for parallel development
worktrees_dir.worktreesDirectory for worktrees (relative to project root)
cleanup_worktrees_on_completefalseAutomatically remove worktrees when tasks complete

Combining with Parallel Agents

Worktrees only really pay off when you also bump max_parallel_agents:

[orchestrator] max_parallel_agents = 4 use_worktrees = true

With this, up to four engineer agents can work on independent tasks in parallel, each in its own worktree.

Best Practices

  • Use meaningful task titles — Branch names are derived from task titles
  • Enable auto-cleanup for short-lived work — Set cleanup_worktrees_on_complete = true if you don’t need to inspect branches after they merge
  • Keep worktrees for human review — Disable cleanup if you want to review agent changes before merging
  • Watch disk space — Multiple worktrees can consume meaningful disk for large repos (each is a full working copy)

Worktrees share Git objects, so most of the storage is cheap — but the working files themselves are duplicated per worktree.

Troubleshooting

Worktree not created

  • Make sure use_worktrees = true in your config
  • Verify your project is a git repository

Permission errors

  • Check that the worktrees directory is writable
  • Ensure git can create branches

Cleanup issues

  • Manually remove worktrees: git worktree remove .worktrees/<name>
  • List all worktrees: git worktree list

Merge conflicts between parallel branches

  • The merge resolver agent handles most conflicts automatically
  • Persistent conflicts escalate to the Blocked lane for human resolution
Last updated on
AgentLoop — Multi-agent loops you can see and control