blog / guides
Git worktrees for AI coding agents: a practical guide
Two agents in one checkout will collide. How git worktrees give each agent its own branch and folder: commands, naming, env files, merging, cleanup, pitfalls.

Run two AI coding agents in the same folder and sooner or later one of them breaks the other. Claude Code is halfway through a refactor when Codex switches the branch. Or one agent runs the formatter over files the other is still editing. Git worktrees fix this, and they come with git already.
Why two agents in one checkout collide
A normal clone has exactly one working directory, one checked-out branch and one index (the staging area). Every process working in that folder shares all three. Agents don't coordinate, so:
git checkoutorgit switchby one agent changes the files under the other one.git add -Aandgit commitsweep up the other agent's unfinished edits.- Build output, lockfiles and caches get rewritten under a running dev server.
- When something goes wrong, you can't tell which agent made which change.
Prompting the agents to "be careful" doesn't solve it. Giving each one its own directory does.

What a worktree is
A worktree is an extra working directory attached to the same repository. Every worktree has its own checked-out branch, its own index and its own files on disk, but they all share one .git object store: one history, one set of branches, one set of remotes. A commit made in one worktree is visible from the others right away, because it's the same repository.
So one task gets one branch, one folder and one agent.
The commands you actually need
Create a worktree on a new branch, based on main:
git worktree add -b fix-cart-total ../shop-fix-cart-total main
-b creates the branch, the path is where the new folder goes, and the last argument is the starting point. If the branch already exists, leave out -b:
git worktree add ../shop-fix-cart-total fix-cart-total
See what you have:
git worktree list
Remove a worktree when you're done with it:
git worktree remove ../shop-fix-cart-total
remove refuses a worktree that has uncommitted or untracked changes. That's a safety net, so treat it as one: commit or throw away the changes on purpose before you reach for --force. If you deleted a worktree folder by hand, git still remembers it. Clean up those stale entries with:
git worktree prune
Naming and where to put them
Keep worktrees next to the repository, not inside it. A worktree inside the repo shows up as untracked files, and file watchers, search and test runners crawl it too. A simple rule that holds up: <repo>-<branch> as a sibling folder.
~/code/shop main checkout
~/code/shop-fix-cart-total agent 1
~/code/shop-add-search agent 2
Name branches after the task (fix-cart-total) so git worktree list reads like a to-do list.
Dependencies and env files per worktree
A new worktree contains only tracked files. That catches people out, because a lot of what makes a project run isn't tracked:
- Dependencies.
node_modules,.venv,vendor/are gitignored, so run your install (npm install,pnpm install,uv sync…) in each new worktree. - Env files.
.envand.env.localaren't copied. Copy them yourself (cp ../shop/.env .) or keep a small setup script that does it. Never commit them to make this easier.
Merging the work back
A worktree branch is an ordinary branch. Review it, then merge it from the main checkout:
cd ~/code/shop
git diff main...fix-cart-total # what the branch changed since it split off
git merge --no-ff fix-cart-total
--no-ff keeps a merge commit, so the agent's work stays grouped and is easy to revert as one piece. If main moved on while the agent worked, merge main into the worktree branch first and let the agent fix any conflicts in its own folder. Or push the branch and open a pull request.
Cleaning up
After the merge:
git worktree remove ../shop-fix-cart-total
git branch -d fix-cart-total
git branch -d (lower-case) refuses to delete a branch that isn't merged, which is the safe default. Run git worktree prune now and then.
Common pitfalls
- A branch can be checked out in only one worktree. If
mainis checked out in your main folder,git worktree add ../x mainfails. Create a new branch frommaininstead. - Untracked files don't come along.
.env, local config and dependencies have to be set up in every worktree (see above). - Ports clash. Two dev servers both want port 3000. Give each worktree its own port, for example
PORT=3001in that worktree's.env. - Shared local services. Worktrees share the repo but not your database. Two agents running migrations against the same local database will still collide.
- Deleting folders by hand. Use
git worktree remove. If you've already deleted one, runprune.
How Bettr handles worktrees
Bettr, a terminal for running AI coding agents side by side, builds this workflow into the UI.
In the project sidebar, worktrees show up as branches under their repo. New worktree… in a project's menu creates <repo>-<branch> next to the repo. Bettr fetches first, so branching off origin/main means today's main, and you can open a session for an agent right there. The sidebar names each worktree by its folder.
Worktrees in the sidebar (or Review on a worktree row) docks the Branches panel, which works as a review station. Every worktree gets a card showing ahead/behind, the files and +/− changed against the merge base (uncommitted and untracked files included), the agent's status and any PR. Review opens the diff with line comments, and Send to agent sends those comments to the agent in that pane. Sync base merges the base branch into the worktree. Merge into base runs merge --no-ff in the base checkout, refuses when that checkout is dirty and aborts cleanly on a conflict. Discard removes the worktree (but refuses if it's dirty) and deletes the branch only if it's merged.
Frequently asked questions
Do worktrees use a lot of disk space? Only for the files you check out, plus whatever you install. History is shared through one .git store, so a worktree costs far less than a second clone. Dependencies like node_modules are usually what takes up the space.
Can two worktrees use the same branch? No. Git allows a branch to be checked out in one worktree at a time. Create a new branch for each task, which is the right model for parallel agents anyway.
Do AI agents need special support for worktrees? No. To Claude Code, Codex or any other CLI agent, a worktree is just a folder with a git repository in it. Start the agent in that folder and it works there.


