DEV Community

Penloom Studio
Penloom Studio

Posted on

Run two AI coding agents at once without them overwriting each other: `git worktree`

Result first: two "agents" edited the same file, app.txt, at the same moment, each in its own folder on its own branch. The main checkout stayed untouched. When I tried to check out the same branch a second time, git refused: fatal: 'agent-a' is already used by worktree.

git worktree add -b agent-a ../wt-a      # folder + branch for agent A
git worktree add -b agent-b ../wt-b      # folder + branch for agent B
git worktree list                        # see all of them
git worktree remove ../wt-a              # clean up when A's branch is merged
Enter fullscreen mode Exit fullscreen mode

What the docs say: "A git repository can support multiple working trees, allowing you to check out more than one branch at a time." git also has a safeguard: by default it won't create a worktree for a branch that "is already checked out by another worktree" (--force overrides that).

Why this matters for agents:

  1. Give each agent its own worktree and branch. They never share uncommitted files.
  2. Review each branch as a normal diff or pull request, one at a time.
  3. All worktrees share one repository, so commits made in one are visible to the others without pushing. That saves disk space compared with full clones, but it also means a careless git branch -D in one folder affects every worktree.

Do this now: before your next multi-agent session, create one worktree per agent and point each agent at its own folder.

Sources


From Penloom Studio's free AI lessons: penloomstudio.com

Top comments (0)