The Problem with Shared Checkouts

When running multiple AI coding agents—or working on a feature while reviewing a PR locally—sharing a single Git working directory quickly breaks down. As soon as one agent switches branches, stages files, or runs a build, it stepped on the other’s working tree.

Cloning the repository multiple times works, but it duplicates .git history and objects across disk.

Isolating Agents with git worktree

git worktree lets you attach multiple independent working directories to the same local .git repository. Each worktree checks out its own branch and has its own index and working tree, while sharing commits, remotes, and reflogs.

1. List Existing Worktrees

From your main repository checkout:

git worktree list

2. Create a Worktree for an Agent

To spin up an isolated directory ../agent-worktree-ai on a new branch feature/ai-task (use -b to create a new branch, or pass an existing branch name):

# Create a new branch in a separate directory
git worktree add -b feature/ai-task ../agent-worktree-ai

Running git worktree list now shows both directories:

/path/to/my/main/repo          3a1f9c2 [master]
/path/to/agent-worktree-ai     3a1f9c2 [feature/ai-task]

You can point an agent session directly at /path/to/agent-worktree-ai. It can edit files, run tests, commit, and push without touching your main checkout.

3. Clean Up When Done

Once the branch is merged or no longer needed, remove the worktree from your main repository directory:

cd /path/to/my/main/repo
git worktree remove ../agent-worktree-ai

If the worktree has uncommitted files you want to discard, pass --force.