Guides

Git worktrees for AI coding agents

Coding agents edit files, run tests and make commits on their own. Give two of them the same folder and they get in each other's way. Git has had a built-in answer since version 2.5: worktrees. This guide explains what a worktree is, the handful of commands you need, the details that catch people out, and how Yami uses worktrees for its agents.

What a worktree is

Every git repository has a main working tree: the folder with your files, where .git lives. git worktree lets you attach more working trees to the same repository, each in its own folder. A linked worktree has its own files, its own staging area and its own checked out branch, but it shares everything else with the main one: the commits, the branches, the tags, the remotes and the config.

Compared with a second clone, a worktree costs only the checked out files, not another copy of the history. A commit made in one worktree is visible from all the others at once, with no push or fetch in between. That is what makes worktrees a good fit for agents: each gets a separate desk, but they all work in one repository.

The commands you need

Create a worktree with a new branch, starting from main:

git worktree add -b agent/api ../myapp-api main

Check out a branch that already exists:

git worktree add ../myapp-fix fix/typo

Make a throwaway worktree with no branch at all (a detached HEAD), for a quick experiment:

git worktree add --detach ../myapp-try HEAD

See every worktree, with its folder, commit and branch. Scripts should use the --porcelain form, which is stable to parse:

git worktree list
git worktree list --porcelain

Remove a worktree when you are done. Git refuses if it has uncommitted or untracked files; --force removes it anyway and discards them:

git worktree remove ../myapp-api
git worktree remove --force ../myapp-api

If a worktree folder was deleted by hand, git still has a record of it. prune clears those records:

git worktree prune

Two more are handy now and then. git worktree move moves a worktree to a new folder, and git worktree lock keeps a worktree from being pruned, for example while it sits on a drive that is not always connected:

git worktree move ../myapp-try ../myapp-experiment
git worktree lock --reason "on an external drive" ../myapp-fix

Why agents need separate checkouts

A person working in a folder knows what they changed. An agent assumes the files on disk are the result of its own edits. When two agents share a folder, that assumption breaks in several ways:

With one worktree per agent, each agent's view of the files is its own, and its branch records exactly what it did. You can review each branch on its own and merge them one at a time. The guide on running several Claude Code agents in parallel walks through that whole flow.

Details that catch people out

A small script for several agents

To set up one worktree per task in one go:

for task in login search settings; do
  git worktree add -b "agent/$task" "../myapp-$task" main
done

Then open a terminal in each folder and start an agent there. Each one commits to its own agent/... branch.

Cleaning up

Worktrees are cheap, but they pile up. After a branch is merged, remove its worktree and delete the branch:

git worktree remove ../myapp-login
git branch -d agent/login

To see which agent branches are already merged, list them against main. A + in front of a name means that branch is still checked out in a worktree:

git branch --merged main --list 'agent/*'

git branch -d refuses to delete a branch that is not merged, which keeps unreviewed work safe; -D deletes it anyway. Finish with git worktree prune and git worktree list to check that only the worktrees you still use are left.

How Yami automates it

Yami is a free desktop app for macOS (Apple silicon) and Windows 11. In a venture, a leader agent writes a plan and up to eight agents work on its tasks at once (four by default, set in Settings). Yami does the worktree work for them:

Let the worktrees take care of themselves

Yami is free and in a private beta, invite only. If you would like to try ventures on your own repository, you can request an invite on the home page.

Request an invite

More guides