A git worktree gives each AI coding agent its own folder and its own branch from one repository, so two agents can edit at the same time without overwriting each other. On Windows the idea works, but six things break that guides written on a Mac never mention: locked files, long paths, branch rules, missing dependencies, WSL path mismatches, and shared ports and databases. This guide covers each one with the fix.

What a worktree isolates, and what it does not

A worktree is a second working folder attached to the same .git data. Each one has its own checked-out branch, index and files.

Isolated per worktree Shared by all worktrees
Files on disk Commits, branches and tags
Checked-out branch and HEAD The stash
Staging area Repository config
Ports, databases, containers, your .env secrets

The git documentation states the rule that surprises people first: a branch can only be checked out in one worktree at a time, and all refs are shared (git-worktree). The last row of the table is not git's business at all, which is why it causes the most trouble.

Problem 1: "Permission denied" when removing a worktree

On Windows a file that any process has open cannot be deleted. git worktree remove then fails halfway and leaves a folder behind. The usual holders are:

  • a dev server or test watcher started by the agent
  • a terminal whose current folder is inside the worktree
  • an editor with a file open
  • antivirus scanning node_modules

Fix, in this order:

# 1. Leave the folder in every terminal, and stop what the agent started
cd C:\src\myapp
Get-Process node, python -ErrorAction SilentlyContinue | Where-Object { $_.Path -like "C:\src\myapp-wt\*" }

# 2. Remove the worktree, then clear git's record of it
git worktree remove C:\src\myapp-wt\fix-login
git worktree prune

If the folder is still locked, close the editor window for that folder and retry before reaching for --force. This is a known pain: users have reported worktree removal failing on file locks in Claude Code issue 41740.

Problem 2: paths that are too long

Windows limits many paths to 260 characters. A worktree nested inside a deep folder, with node_modules inside that, passes the limit quickly and you get "Filename too long".

Two fixes, use both:

git config --global core.longpaths true

Then keep worktrees in a short sibling folder near the drive root, such as C:\src\myapp-wt\fix-login, never inside the repository and never under a synced folder like OneDrive. Sync clients lock files and multiply every problem in this guide. On Windows 11, a Dev Drive is built for this kind of workload and reduces antivirus overhead on it.

Problem 3: "already checked out" errors

Each branch belongs to one worktree. Asking an agent to "switch to main" inside a worktree fails if main is checked out in your primary folder. Give every agent a new branch at creation time:

git worktree add -b agent/fix-login C:\src\myapp-wt\fix-login origin/main

Windows adds one more trap: file names are not case sensitive, so branches named Fix-Login and fix-login collide. Pick lowercase names and keep to them.

Because the stash is shared, never let agents run a bare git stash pop. One agent can pop another agent's changes into the wrong folder. A temporary commit on the agent's own branch is safer.

Problem 4: the new worktree cannot run the app

A worktree is a fresh checkout. Anything git ignores is missing: node_modules, build output, and your .env file. Anthropic's documentation says the same: initialize your development environment in each worktree (Claude Code worktrees).

  • Dependencies. Run the install in each worktree. A package manager with a shared store, such as pnpm, keeps disk use low. Cursor's documentation advises against symlinking dependencies into worktrees (Cursor worktrees).
  • Secrets. Claude Code copies files listed in a .worktreeinclude file into worktrees it creates, which is the clean way to bring .env along. For other tools, copy it in a setup script.

Problem 5: mixing Windows git and WSL git

A worktree records the absolute path of its parent repository. Create it from PowerShell and that path looks like C:/src/myapp. Open the same folder from WSL, where the path is /mnt/c/src/myapp, and git reports that it is not a repository.

The rule: one side per repository. If the agent runs in native Windows, create and use its worktrees from Windows. If it runs in WSL, keep the repository inside the Linux file system and do everything there. git worktree repair can fix a moved worktree, but it will not make one worktree work from both sides at once.

Problem 6: ports, databases and other shared things

Two agents that both start the app on port 3000 will fight. Two agents that both run database migrations will corrupt each other's work. No official worktree documentation covers this, because git does not manage it. What works in practice:

  1. Give each worktree its own port through an environment variable rather than a hardcoded value.
  2. Run only one agent at a time on tasks that change the database schema, or give each worktree its own local database.
  3. When a task ends, stop its dev server before removing the worktree. Orphaned servers are what hold the file locks from problem 1.

A checklist before you start a parallel run

  • core.longpaths is on
  • Worktrees live in a short sibling folder, outside any synced folder
  • Each agent gets a new lowercase branch
  • A setup step installs dependencies and copies .env
  • Each worktree has its own port
  • Schema-changing tasks do not run in parallel
  • Cleanup stops processes first, then runs git worktree remove and git worktree prune

How CPLai handles this

CPLai is a Windows app, so this is the environment it is built for. A development workspace creates the branch and a sibling worktree for a task, opens a terminal in it, and keeps the record after the terminal or the app closes. Each workspace can lease a preview port that CPLai checks against its own ledger and the live TCP port before assigning it, then injects as PORT, so two workspaces do not collide. Archiving a workspace releases its ports and keeps the branch and files.

One limit to know: CPLai does not give each workspace its own database today. The rule in problem 6 still applies, and per-workspace database isolation is on our roadmap.

Sources