Blog · Guide · 27 November 2026

Git Worktrees for Parallel Agents: Ports, Env Files, Cleanup

Git worktrees for parallel agents give each agent its own directory and branch while sharing one repository's history, which is why they are the usual first step for running coding agents side by side. Create one per agent with git worktree add (or claude --worktree), copy the gitignored env files each one needs, give each worktree its own port and test database, and remove worktrees and branches when the work is merged. Worktrees isolate files only: ports, databases, caches and the overlap between agents' changes still need handling. This guide covers each of those.

What a worktree isolates, and what it does not

Git's documentation: "A git repository can support multiple working trees, allowing you to check out more than one branch at a time." A new worktree is "linked to the current repository, sharing everything except per-worktree files such as HEAD, index, etc."

Isolated per worktree Shared between worktrees
Working files The .git object store and branches (refs/)
HEAD (checked-out branch) Remotes and config
The index (staging area) Anything outside the directory: ports, databases, Docker, caches in your home directory, the network
Untracked files, including node_modules Stashes

The right-hand column is where parallel agents still collide. Capy's write-up on parallel agents puts it well: "Parallel coding agents collide over more than files", listing lockfiles, migrations, databases, ports (both agents want 3000) and branch names.

Git also refuses, by default, to check out a branch that is already checked out in another worktree. That is a feature: two agents on one branch would undo each other.

Creating git worktrees for parallel agents

Tessl's guide to parallelising agents calls worktrees the mechanism git already has "integrated" for this: each agent gets a copy that can be merged back later. By hand:

git worktree add ../app-agent-1 -b agent/search-ranking
git worktree add ../app-agent-2 -b agent/billing-cents
git worktree list

With Claude Code:

claude --worktree search-ranking

Claude Code's docs say this creates the worktree under .claude/worktrees/search-ranking/ on a new branch worktree-search-ranking, branched by default from the remote's default branch (worktree.baseRef: "fresh"; set "head" to branch from your current work). Add .claude/worktrees/ to .gitignore so the worktrees do not show up as untracked files in your main checkout. Subagents can get their own worktree with isolation: worktree in their frontmatter.

Naming

Pick names you can read in git branch a week later: agent/<task> or <agent>/<task>. Branch names collide too: two agents both creating fix-tests will reject or overwrite each other's pushes. Derive names from the task, not from the agent's guess.

Env files and .worktreeinclude

A worktree is a fresh checkout, so gitignored files such as .env are not there. Claude Code reads a .worktreeinclude file at the project root, in .gitignore syntax, and copies matching files into each new worktree. Only files that match and are gitignored are copied, so tracked files are never duplicated:

.env
.env.local
config/secrets.json

For worktrees you create yourself, a short script does the same:

#!/bin/sh
# new-agent-worktree.sh NAME
set -e
name=$1
git worktree add "../app-$name" -b "agent/$name"
cp .env.local "../app-$name/.env.local"

Keep secrets out of committed files either way. An agent that can read .env can print it into a log or a commit, so give agents development credentials only, and check whether each agent needs the file at all.

Ports and databases: derive them from the name

Two dev servers on port 3000 is the first collision most people hit. Give each worktree a stable offset derived from its name, so it is the same every time:

# in each worktree's setup
slot=$(printf '%s' "$(basename "$PWD")" | cksum | cut -d' ' -f1)
export PORT=$((3000 + slot % 500))
export DATABASE_URL="postgres://localhost/app_test_$(basename "$PWD" | tr -c 'a-zA-Z0-9\n' '_')"

Write these into the worktree's .env.local once, so every command the agent runs picks them up. The same goes for anything else bound to a fixed name: Docker Compose project names (COMPOSE_PROJECT_NAME), Redis database numbers, temporary directories.

One test database per worktree matters more than one port. Two agents running migrations against the same database produce failures neither can explain. If your tests create and drop their own database, make sure the name includes the worktree.

Dependencies and caches

Each worktree has its own node_modules or virtualenv, which costs disk and install time. Package managers with a shared content store (pnpm, uv) make extra worktrees cheap. Build caches in your home directory are shared, which is usually fine; a cache that two concurrent builds write to is not. If builds start failing only when two agents run at once, suspect a shared cache.

Lockfiles deserve their own rule: let one agent change dependencies, and have the others wait for that change to land. Two agents each running an install rewrite the lockfile in ways that conflict badly.

Cleanup

Git's docs: "Only clean worktrees (no untracked files and no modification in tracked files) can be removed." Use --force for the rest:

git worktree remove ../app-agent-1            # refuses if unclean
git worktree remove --force ../app-agent-1    # discards changes
git worktree prune                            # forget worktrees whose directories are gone
git branch -d agent/search-ranking            # after it is merged

Claude Code removes a clean worktree from an unnamed interactive session on exit, and asks when it holds work. Runs with -p have no exit prompt, so their worktrees are left behind; Claude Code's docs say to remove them with git worktree remove, after git worktree unlock if git refuses because the worktree is locked. A weekly git worktree list is a good habit.

Where worktrees stop

Worktrees keep agents from trampling each other's files. They do not tell anyone that two agents are changing the same function in their separate copies; that shows up at merge time, after both have finished. Two additions close the gap:

  1. Claims. An agent records which paths it is changing for a while; another agent's edit under that claim is refused before it happens. Git worktrees vs file leases compares the two.
  2. A merge check before stopping. Each agent sends its change and the commit it started from, and the change is compared with its peers' in the same fan-out. Forkit keeps that base commit in .git/forkit-base the first time it sees a checkout, so a commit made in the worktree does not move the base or hide the work. Changes to the same or adjacent base lines are reported as conflicts with the peer's name, while the agents can still fix them.

When a repository per agent is better

Worktrees need every agent on one machine. Agents in cloud sandboxes each have their own disk, so a worktree does not help. There, give each agent its own repository: Forkit can create a fork per fan-out agent from a public repository or from a branch you push, with a short-lived write token, and forkit fanout collect --merge brings every agent's branch back into one local repository. A repository per agent covers it.

Checklist

  • One worktree and branch per agent, named after the task.
  • .claude/worktrees/ in .gitignore if you use --worktree.
  • .worktreeinclude (or a script) for env files; development credentials only.
  • A port, database and Compose project name derived from the worktree name.
  • One agent owns dependency changes.
  • Claims for shared files and a merge check before agents stop.
  • git worktree remove, prune and branch deletion after merging.

More on running several sessions in how to run Claude Code in parallel. Forkit has a Free plan; Pro is $20 a month and Team $40 per seat a month, billed per person, never per agent. More at forkit-mcp.com.

Sources

Read on 27 Nov 2026: