Skip to content
codee3.dev/guides/git-worktrees-ai-agents

Git worktrees for AI agents: a step-by-step setup on Windows

How to give Claude Code, Codex or Gemini CLI each their own git worktree, set each one up, and merge the work back without clashes.

Guide6 min readUpdated 2026-10-21gitworktreeswindows
THE SHORT ANSWER

Give each AI agent its own git worktree: a second folder of the same repository, on its own branch, made with git worktree add. Agents then can’t overwrite each other’s files, and every worktree shares one history. Install dependencies in each, start one agent per folder, and merge each branch once you’ve reviewed it.

What is a git worktree?

A git worktree is a second working folder for a repository you already have. Each worktree has its own branch and its own files on disk, but they all share one history, so a commit made in one is visible to the others straight away. It’s built into Git; there’s nothing to install.

For AI agents that’s exactly the right shape. An agent works in its folder, on its branch, and can run its own tests there. Another agent in another worktree can’t see its half-finished edits, and neither can touch your main folder.

1. Make one worktree per agent

From your main folder, add a worktree beside it for each job, each on a new branch. Name the folder and the branch after the job, not the agent: the job is what you’ll review later.

PowerShell
PS C:\code\shop> git worktree add ..\shop-booking -b booking
PS C:\code\shop> git worktree add ..\shop-emails -b emails
PS C:\code\shop> git worktree list
C:/code/shop a1f3c2d [main]
C:/code/shop-booking a1f3c2d [booking]
C:/code/shop-emails a1f3c2d [emails]

Each new branch starts from the commit you’re on. A branch can be checked out in only one worktree at a time; if Git refuses, that branch is already open somewhere else, and git worktree list shows where.

2. Set each worktree up

A worktree holds the files Git tracks, and nothing else. Anything your .gitignore leaves out isn’t there yet:

  • Install dependencies again in each worktree (npm install, pip install and so on).
  • Copy across any local settings the app needs, such as a .env file. Never commit it to make this easier.
  • If two worktrees run a dev server, give each its own port, and its own database or data folder if the app writes one, so they don’t fight over them.

On Windows, deep folders in a worktree can hit the old 260-character path limit. Git for Windows can lift it for you:

PowerShell
PS C:\code\shop> git config --global core.longpaths true

3. Start one agent in each

Open a terminal in each worktree and start the agent there. Claude Code, Codex and Gemini CLI all work on the folder they’re started in, so each one stays inside its own copy.

PowerShell
PS C:\code\shop-booking> claude
# in a second terminal
PS C:\code\shop-emails> codex

Give each agent one job, and say which branch it’s on. If the agents should follow the same rules, keep those rules in a file the repository tracks, so every worktree has them: see a decision log for agents.

4. Merge, then clean up

When an agent says it’s finished, read the change and run the tests in its worktree. Then merge it from your main folder, remove the worktree, and delete the branch. Merge one branch at a time, and rebase the next on top before merging it, as the parallel agents guide shows.

PowerShell
PS C:\code\shop> git merge booking
PS C:\code\shop> git worktree remove ..\shop-booking
PS C:\code\shop> git branch -d booking

git worktree remove refuses while the folder still has changes nobody committed, which protects work you haven’t looked at. If you ever delete a worktree folder by hand instead, run git worktree prune so Git forgets it.

Mistakes to avoid

checklist
- Two agents in one worktree: they edit each other’s files again.
- Worktrees inside the main folder: tools that scan it see every copy.
- Old worktrees left lying around: list them weekly, remove the merged ones.
- Merging everything at the end: merge each job as it finishes.
With Founder, each helper the orchestrator starts gets its own worktree, in your user folder under .codee3\worktrees, on its own codee3/… branch. When you land a helper’s work, Codee3 runs the project’s checks, makes a checkpoint you can undo, merges it, and removes the worktree and its branch.

The free IDE runs Claude Code, Codex and Gemini CLI side by side on up to two projects open at a time, and you can follow every step above in its terminal. Helpers handed out by the orchestrator, and landing their work in one step, are Founder.

Worktrees without the typing.Free for two projects open at a time: each agent you start gets its own worktree, and its work lands when you say. Founder adds an orchestrator that starts the helpers for you.Download for Windows