Sub topic 6
Giving each agent its own worktree
This chapter assumes you are comfortable with everyday Git (commit, branch, merge, diff) and basic shell use, including background processes. It also assumes you have run a command-line coding agent on a single task. You should already know the commands from the earlier chapters: git worktree add, list, and remove. Here you turn them into a repeatable setup step that gives every agent a clean, isolated place to work.
The goal of the setup step
Before an agent starts, three things must be true:
- The agent has its own directory, so its file edits cannot touch another agent's files.
- The agent has its own branch, so its commits cannot land on another agent's history.
- Both start from the same known commit, so you can compare and merge the results later.
A linked worktree provides the first two. The third depends on which start point you pass to git worktree add. The setup step is mostly about making these three choices deliberately instead of by accident.
Choosing names and locations
Two conventions make a pile of worktrees manageable.
Branch names use a prefix. A prefix such as agent/ groups agent branches in git branch output. It also lets you list or delete them in bulk, for example git branch --list 'agent/*'. Name the branch after the task, not after the agent or the date: agent/auth tells you what is in it.
Worktree directories are siblings of the repository. If the repository is ~/code/myrepo, put worktrees at ~/code/myrepo-auth, ~/code/myrepo-billing, and so on. Do not create them inside the repository. A worktree nested in the main checkout shows up as untracked files in git status. Search tools and agents running in the parent may also read it, which defeats the isolation you are building.
The directory name has no technical link to the branch name. It is only a label, so choose one you can recognize in a terminal tab title or an editor window.
Creating one worktree per agent
The basic form creates the branch and the directory in one command:
git worktree add -b agent/auth ../myrepo-auth main
git worktree add -b agent/search ../myrepo-search main
git worktree listRead the arguments from left to right. -b agent/auth creates a new branch with that name. ../myrepo-auth is the directory to create. main is the start point, the commit the new branch begins at. git worktree list then prints each worktree with its path, its current commit, and its branch.
Pick the start point on purpose
The start point decides what every agent sees on its first read of the code. Three situations come up often:
| Situation | Start point | Why |
|---|---|---|
| Local `main` is current | `main` | Simple and fast. |
| Others may have pushed since your last pull | `origin/main` after `git fetch` | Agents begin from the latest shared code. |
| Agents must build on a feature in progress | That feature branch | All agents share the same base, and merging back targets that branch. |
Uncommitted changes in your main checkout are not carried over. A new worktree is built from a commit, so anything you have only in your working directory is invisible to the agents. Commit it first, or stash it, if the agents need it.
Avoid accidental upstream tracking
When the start point is a remote-tracking branch such as origin/main, Git may set the new branch to track it. The new agent/auth branch would then appear to be "behind" origin/main whenever someone else pushes. A later plain git push may also behave in surprising ways. Pass --no-track to avoid this:
git fetch origin
git worktree add --no-track -b agent/auth ../myrepo-auth origin/mainYou can set a proper upstream later, when you push the branch for review.
Giving the agent its task
An agent needs a written brief. The cleanest place for it is a file in the worktree, which the agent can read with its normal file tools. Two points matter here:
- Keep each brief out of version control. If you commit
TASKS.mdtomainbefore creating the worktrees, every agent sees every other agent's task. That invites scope creep. A per-worktree brief file that Git ignores avoids both that and a merge conflict on the brief itself. - Ignore the file once, for all worktrees. The file
info/excludeinside the shared.gitdirectory works like.gitignore, but it is not tracked. Every linked worktree honors it.
common=$(git rev-parse --git-common-dir)
echo "AGENT_TASK.md" >> "$common/info/exclude"Now you can write AGENT_TASK.md into any worktree and it will never show up in git status or get committed by an agent that runs git add -A.
A setup script
Doing this by hand for three agents is tolerable. For six, you will make a typo. The script below takes a base ref and a list of task names. It creates a worktree and a branch for each one, skips any that already exist, and copies in a brief if you wrote one.
#!/usr/bin/env bash
set -euo pipefail
if [ "$#" -lt 2 ]; then
echo "usage: $0 BASE_REF TASK [TASK...]" >&2
exit 1
fi
base=$1
shift
repo=$(git rev-parse --show-toplevel)
name=$(basename "$repo")
parent=$(dirname "$repo")
common=$(git rev-parse --git-common-dir)
# Ignore the brief file in every worktree, once.
touch "$common/info/exclude"
grep -qx 'AGENT_TASK.md' "$common/info/exclude" \
|| echo 'AGENT_TASK.md' >> "$common/info/exclude"
for task in "$@"; do
branch="agent/$task"
path="$parent/$name-$task"
if [ -e "$path" ]; then
echo "skip $task: $path already exists" >&2
continue
fi
if git show-ref --verify --quiet "refs/heads/$branch"; then
echo "skip $task: branch $branch already exists" >&2
continue
fi
git worktree add --no-track -b "$branch" "$path" "$base"
if [ -f "$repo/briefs/$task.md" ]; then
cp "$repo/briefs/$task.md" "$path/AGENT_TASK.md"
else
printf 'Task: %s\nDescribe the work here.\n' "$task" > "$path/AGENT_TASK.md"
fi
done
git worktree listRun it as ./setup-agents.sh main auth billing search. The briefs/ folder is an optional convention. Keep it untracked, or in a location outside the repository, so the briefs do not leak into agent branches.
The script is safe to re-run. It skips names that already exist instead of failing halfway through, so you can add a fourth task later without disturbing the first three.
Errors you will meet
The setup step fails in a small number of predictable ways.
fatal: 'main' is already checked out at '...'. A branch can be checked out in only one worktree at a time. You see this when you try to check out main a second time. If an agent needs a copy of main without owning the branch, create a detached worktree:
git worktree add --detach ../myrepo-scratch mainFor parallel agents you rarely need this. Give each one a new branch, as shown above.
fatal: a branch named 'agent/auth' already exists. A previous run left the branch behind. Decide whether it holds work you want. If it does, attach a worktree to the existing branch without -b: git worktree add ../myrepo-auth agent/auth. If it does not, delete it with git branch -d agent/auth, or -D if it is unmerged and you are sure.
fatal: '../myrepo-auth' already exists. The directory is there, and it is not empty. Git refuses to overwrite it. Check what it is before you delete anything. It may be a stale worktree, in which case git worktree list shows it, or an unrelated folder.
The agent cannot find node_modules, .env, or a virtual environment. A new worktree contains only tracked files. Anything ignored or untracked in your main checkout is absent. The next chapters cover dependencies, ports, and local configuration. For now, expect to copy or reinstall these before the agent can run tests.
Protecting worktrees you want to keep
If a worktree lives on a removable drive or a network share, Git may treat its record as stale when the path disappears, and git worktree prune will delete that record. Lock the worktree to prevent that:
git worktree lock --reason "long-running agent job" ../myrepo-auth
git worktree list
git worktree unlock ../myrepo-authA locked worktree also refuses git worktree remove unless you force it twice. For ordinary local agents, locking is optional.
A checklist before you start the agents
git worktree listshows one line per agent, each with a distinct branch and the same base commit.git statusin each worktree reports a clean tree.- Each worktree contains an
AGENT_TASK.mdthat describes only its own task. - Required untracked files, such as
.env, are in place (see the isolation chapter). - The main checkout is on a branch no agent will use, so it stays available for review and merging.
Exercise
You run git worktree add -b agent/search ../myrepo-search main and then, in the same repository, git worktree add ../myrepo-search2 agent/search. The second command fails. Why, and what are two ways to get a second directory on the same code?
Show answer
A branch can be checked out in only one worktree at a time, and agent/search is already checked out in ../myrepo-search. You can create a detached worktree at that commit with git worktree add --detach ../myrepo-search2 agent/search, or create a new branch from it with git worktree add -b agent/search-2 ../myrepo-search2 agent/search.
Exercise
You have uncommitted edits in your main checkout that the agents need as a starting point. You run git worktree add -b agent/auth ../myrepo-auth main. Will the agent see your edits? What should you do first?
Show answer
No. A new worktree is built from the commit that main points to, so uncommitted edits are not included. Commit the changes, on main or on a feature branch, and use that branch as the start point.