Git worktrees for parallel AI coding agents
A git worktree is a second working folder attached to the same repository, checked out on its own branch. Git's documentation describes the command as a way to "manage multiple working trees attached to the same repository", so you can have more than one branch checked out at a time (git-worktree, read November 2026). That is why coding agents use them: two agents editing the same folder overwrite each other's work, while two agents in two worktrees each get their own files and branch and still share one history.
How a worktree differs from a clone
A clone copies the whole repository. A worktree shares it. Git says a new worktree is linked to the current repository, "sharing everything except per-worktree files such as HEAD, index, etc." Commits, branches and the remote are the same in every worktree, so a commit made on one worktree's branch is visible from the others straight away, with git log <branch>, and no fetch is needed.
Git calls the folder you cloned or created with git init the main worktree, and every folder you add a linked worktree. A repository has one main worktree and any number of linked ones.
The commands you need
The everyday commands:
| Command | What it does |
|---|---|
git worktree add ../hotfix | Creates a new branch hotfix and checks it out at ../hotfix |
git worktree add <path> <branch> | Checks out an existing branch in a new folder |
git worktree add -d <path> | Creates a throwaway worktree on a detached HEAD, with no new branch |
git worktree list | Shows the main worktree first, then each linked one with its branch |
git worktree remove <path> | Deletes a linked worktree; it refuses if the folder has changes, unless you pass --force |
Git's own example makes an emergency fix without touching your current work:
git worktree add -b emergency-fix ../temp master
cd ../temp
# edit, commit
git worktree remove ../temp
If you delete a worktree folder by hand, git worktree prune clears git's leftover records of it. git worktree lock stops a worktree on an external drive or network share from being pruned while it is unplugged.
The one-branch rule
Git won't check out the same branch in two worktrees. The documentation says add "refuses to create a new worktree when <commit-ish> is a branch name and is already checked out by another worktree." OpenAI's Codex docs state the same rule: "If you check out a branch on a worktree, you can't check it out in your local checkout at the same time, and vice versa."
This is why agent tools either create a fresh branch for each worktree or use a detached HEAD. Codex does the second: its worktrees sit in $CODEX_HOME/worktrees "in a detached HEAD state", which "lets Codex create several worktrees without polluting your branches" (Codex worktrees, read November 2026).
Why AI coding agents use worktrees
An AI coding agent works by reading files, editing them and running commands, often for many minutes at a stretch. Put a second agent in the same folder and the two will edit the same files and run tests against each other's half-finished changes. A worktree per agent gives each one a stable view of the code, its own uncommitted changes and its own branch to commit to. When an agent's work is done, you review its branch like any other and merge it or delete it.
The cost is setup time. Each worktree is a fresh checkout, so dependencies have to be installed again and ignored files, such as a .env, aren't there. Claude Code and Codex both read a .worktreeinclude file listing ignored files to copy into new worktrees. Cursor uses a .cursor/worktrees.json file with setup commands such as npm ci, and advises against symlinking dependencies into the worktree (Cursor worktrees, read November 2026).
Worktrees in Claude Code, Codex and Cursor
Claude Code has a built-in flag. claude --worktree feature-auth creates a worktree under .claude/worktrees/feature-auth/ on a new branch named worktree-feature-auth and starts Claude in it. Anthropic suggests adding .claude/worktrees/ to your .gitignore. Subagents can work in their own worktrees too: you can ask Claude to "use worktrees for your agents", or add isolation: worktree to a custom subagent's settings. When you exit, Claude removes an unnamed session's worktree and branch automatically if nothing changed, and asks first for a named session like the one above or for one with changes. Its docs warn that ignored files don't count as changes, "so you can lose them at exit" (Claude Code worktrees, read November 2026).
The isolation has limits. While a session is in a worktree, Claude Code blocks edits and git commands aimed at the main checkout, but the docs note that "a command that writes into the main checkout without running git there, such as cp or a shell redirect, isn't refused." For projects outside git, a WorktreeCreate hook can replace the git steps with your own.
Codex runs "multiple independent chats in the same project" in worktrees, each on a detached HEAD, as described above.
Cursor gives each task in its Agents Window its own checkout: "Each task gets its own files, dependencies, and changes while your main checkout stays untouched." Its docs say the worktree feature "is only available in the Agents Window."
How Neotask Code uses worktrees
Neotask's Code page runs Claude Code, Codex, Cursor or Neotask's own agent on a project. Worktrees come into it in four places.
New threads. Each project has a setting, "New threads start in", with two choices: Local or New worktree. The default for a new project is Local, as it is in the Claude Code desktop app, where "by default, Worktree is off." Set it to New worktree and each new thread gets its own worktree inside the project's managed worktrees/ folder, on a branch named like neotask/thread-<time>, starting from the remote's default branch unless you pick the current branch instead. You can give branches your own prefix, or let the model name each branch from the conversation.
Forks and hand-offs. Forking a thread puts the copy on its own branch and worktree, so the original and the fork can carry on side by side. An agent can also hand its work off to a new worktree mid-thread.
Child agents. When an agent delegates part of a task, the child gets an isolated worktree by default for company or unattended work. Neotask creates it on a detached HEAD at the parent's commit. The parent's uncommitted edits aren't copied, so the child starts from what was committed. If the worktree can't be made, Neotask refuses the delegation rather than letting the child edit the parent's checkout.
Cleanup. A cleanup job runs a few minutes after the app starts and then once a day. It removes clean worktrees of archived or deleted threads with git worktree remove, never a plain folder delete. A worktree with uncommitted or unpushed work, or no upstream branch, stays where it is and is reported instead. The settings screen says it in one line: "Cleanup never removes a worktree with uncommitted or unpushed work." The defaults remove a thread's worktree when you archive the thread, keep the 15 most recent and remove older ones after 30 days. You can change both limits or turn cleanup off.
Agents can create a worktree inside the managed folder without asking, unless you set the project to ask first. A worktree anywhere else needs your approval, except in Full access mode.
Common problems
- Git refuses to add a worktree for a branch. Another worktree already has that branch checked out. Use a new branch, a detached HEAD, or remove the other worktree first.
- Missing environment files. A worktree is a fresh checkout. Copy ignored files with
.worktreeincludeor your tool's setup script. - Stale entries after deleting a folder by hand. Run
git worktree prune. - Submodules. Git's documentation says support for submodules in multiple checkouts "is incomplete", and recommends against several checkouts of a superproject.
Frequently asked questions
Do git worktrees use a lot of disk space? Each worktree holds a full copy of the checked-out files, plus whatever dependencies you install in it, but the history is shared. A worktree of a large repository still costs far less than a second clone.
Can two worktrees be on the same branch? No. Git refuses unless you force it. Agent tools work around this with a new branch per worktree or a detached HEAD.
How do I delete a git worktree? Run git worktree remove <path>. It only removes clean worktrees; add --force to discard changes. The main worktree can't be removed.
Does Claude Code create worktrees automatically? Not for an ordinary session. It creates one when you pass --worktree, ask it to work in a worktree, set isolation: worktree on a subagent, turn on Worktree in the desktop app, or start Remote Control with --spawn worktree.