ReadbackLesson 09In progress
2 steps · 30 minutes · one terminal

AGENTS INPARALLEL

Three agents, one repository. Branches alone are not enough — they share a single working tree, so two agents editing at once corrupt each other’s state. Worktrees give each one its own. And you can compute, before any of them finishes, exactly where they are going to collide.

What you already know

Retrieval · lessons 03 and 08

Two from memory. The first one is why branches alone do not solve this.

Answer before you move on

How many working trees and indexes does an ordinary git repository have?

Answer before you move on

Two branches both changed a file, in different places. Does merging them conflict?

One repository, several working trees

Claim + proof · git worktree add

The obvious way to run three agents on one project is three clones. It works, and it costs you three copies of the history, three sets of remotes to keep in sync, and no way to see one agent’s commits from another without pushing.

git worktree is the alternative: additional working trees, each with its own index and its own checked-out branch, all backed by one object database (git-worktree).

Executed, output verbatim
$ git worktree add -b agent-retry ../wt-retry
$ git worktree add -b agent-parse ../wt-parse
$ git worktree list
/sandbox/l09/app       4e1d742 [main]
/sandbox/l09/wt-parse  4e1d742 [agent-parse]
/sandbox/l09/wt-retry  4e1d742 [agent-retry]

Three directories, three branches, one repository. The first line is the original clone; the other two are new directories beside it.

The mechanism is the plainest thing in this course. In a linked worktree, .git is not a directory — it is a file containing one line.

Executed, output verbatim
$ cat ../wt-retry/.git
gitdir: /sandbox/l09/app/.git/worktrees/wt-retry

Seventy-one bytes. A pointer back to the real repository, which is where every object lives.

What is shared and what is not

Shared: the object database, every branch, every tag, the remotes, the config, and the stash. Not shared: the working tree, the index, and HEAD. So each agent gets its own files and its own staging area, and every commit any of them makes is instantly an object all the others can see.

Predict before you read on

An agent working in wt-retry makes a commit. From wt-parse, with no fetch, no pull and no push, what does git log --oneline --all show?

Executed, output verbatim
$ git log --oneline --all
6301378 Retry five times
4e1d742 Add the app

Run from wt-parse, immediately after a commit made in wt-retry.

And git enforces the one rule that makes this safe: a branch can be checked out in at most one worktree. Two agents cannot end up on the same branch by accident.

Executed, output verbatim
$ git switch agent-retry
fatal: 'agent-retry' is already used by worktree at '/sandbox/l09/wt-retry'

A refusal, naming the directory that holds it. This is the collision that would be worst — two processes rewriting one branch — and it is impossible.

What you still have to duplicate

Git shares its own data; it shares nothing else. Each worktree needs its own node_modules, its own build output, and its own .env — because those live in the working tree, and each worktree has a separate one. Untracked files do not travel between them either, which is usually what you want and occasionally a surprise.

Set this up on a real project

git worktree add -b <branch> ../<dir> from any repository. git worktree list shows every one. git worktree remove ../<dir> cleans up, and git worktree prune tidies after a directory you deleted by hand.

2 of 4 steps remain

Compute the collision

You have read the first 2 steps in full — a claim and the verification that settles it. The rest of this lesson, and the 9 lessons after it, open when subscriptions do. Nothing that is free today will be taken away then.

Read the free reference
Subscriptions open soon
the free preview stays