Sub topic 6
Giving Each Agent Its Own Worktree
This chapter assumes you know everyday Git (commit, branch, merge, diff), basic shell usage, and how to run a command-line coding agent on one task. You will set up one worktree and one branch for each agent, so that agents never write into the same directory.
The setup rule
Use one task, one branch and one worktree for each agent. The branch holds the agent's commits. The worktree is the directory where the agent edits files. Both are created together in one command, from a known starting point such as main.
The commands below create two sibling directories next to the repository. Each directory has a new branch based on main.
git worktree add -b agent/auth ../myrepo-auth main
git worktree add -b agent/search ../myrepo-search main
git worktree listgit worktree add -b agent/auth ../myrepo-auth main does three things:
- Creates the branch
agent/authat the current tip ofmain. - Creates the directory
../myrepo-authand checks the branch out in it. - Registers the new directory with the main repository.
git worktree list then shows each worktree with its path and branch.
Naming conventions
Consistent names make the later steps easier to script and review.
| Item | Pattern | Example |
|---|---|---|
| Branch | `agent/<task>` | `agent/auth` |
| Directory | `../<repo>-<task>` | `../myrepo-auth` |
The agent/ prefix groups the branches in git branch --list 'agent/*'. It also makes cleanup easy, because you can tell at a glance which branches were created for agents.
When the branch is already taken
Git allows a branch to be checked out in only one worktree at a time. This rule protects you from two directories moving the same branch pointer. If you need a second directory on the same commit, you have two options.
git worktree add --detach ../myrepo-scratch main
git worktree add ../myrepo-main2 main # fails if main is already checked out elsewhereThe first command uses a detached HEAD, so no branch is involved. That is fine for throwaway experiments, but commits made there belong to no branch. Create a branch before you keep the work. The second command fails while main is checked out in your main directory. For agents, avoid the problem by always passing -b with a new branch name.
Writing the task down
An agent in a fresh worktree has no memory of your plan. Give each one a short task description that names the goal, the files it may touch, and how to check success. A small file such as TASKS.md works. Commit it to main before you create the worktrees, so every worktree contains it.
What the diagram shows
The diagram shows one repository with its shared history, and two worktrees that each hold their own branch and files.
Common errors
fatal: 'agent/auth' is already checked outoralready exists. The branch or directory was left over from an earlier run. Remove the old worktree and branch first (a later chapter covers cleanup), or choose a new name.- The agent cannot find your
.envfile. Untracked files do not appear in a new worktree. Copy them in, as the next chapters show. - The worktree starts from the wrong commit. The last argument of
git worktree addsets the starting point. Name it explicitly instead of relying on whatever is checked out.
Exercise
You run git worktree add ../myrepo-docs main while main is checked out in your main directory. What happens, and what is the one-command fix that gives the docs agent its own branch?
Show answer
Git refuses, because a branch can be checked out in only one worktree at a time. Use git worktree add -b agent/docs ../myrepo-docs main, which creates the new branch agent/docs from main and checks it out in the new directory.