Sub topic 1
Why Parallel Agents Collide in One Checkout
Prerequisites
Assume you know everyday Git commands such as commit, branch, merge, and diff. You should also be comfortable with basic shell commands and running a coding agent from a terminal.
Why One Checkout Collides
A coding agent changes files in its current working directory. If several agents use the same checkout, they all edit the same files. One agent can overwrite another agent's uncommitted changes. They can also change the active branch, index, or generated files while another agent is testing.
This is a coordination problem, not an agent-specific bug. Each task needs its own working directory and branch.
How a Worktree Relates to Its Repository
A Git worktree is another checkout connected to the same repository. The worktrees have separate files, indexes, and HEAD references, so agents can edit and commit independently. They share Git objects, references, and repository history, so creating a worktree is cheaper than cloning the repository.
| Item | Shared or separate |
|---|---|
| Working files | Separate |
| Branch checkout | Separate |
| Git object database | Shared |
| Commit history | Shared |
Creating, Listing, and Removing Worktrees
Run these commands from the repository:
git worktree add -b agent/auth ../myrepo-auth main
git worktree add -b agent/search ../myrepo-search main
git worktree list
git worktree remove ../myrepo-authThe first two commands create directories beside the current repository. Each starts from main and creates a new branch. git worktree list shows registered worktrees. Remove a worktree only after its changes are committed or safely copied elsewhere.
One Branch per Worktree
A branch can be checked out in only one worktree at a time. This prevents two working directories from silently moving the same branch pointer.
Use a detached worktree when you need files at a commit but do not need a branch:
git worktree add --detach ../myrepo-scratch mainA detached HEAD points directly to a commit. It is useful for inspection or temporary experiments, but create a branch before making work you intend to keep.
Exercise
Why should two parallel agents use different branches?
Show answer
Each worktree can then advance its own branch without changing the branch checked out by another agent.