Dev tools
Git Worktree Guide: Run Parallel AI Coding Agents
A git worktree gives each branch its own folder from one repo, so several AI coding agents can work at once. Commands, Claude Code setup and pitfalls.
A git worktree is an extra working folder attached to the same Git repository, with its own branch checked out. Instead of switching branches in one folder, or cloning the repo again, you get several folders that share one history: one for main, one for a feature, one for a bug fix. That makes worktrees the standard way to run several AI coding agents at once, because each agent gets its own files to edit while all their commits land in the same repository.
This guide explains what a git worktree is, git worktree vs branch, the commands you need (git worktree add, list and git worktree remove), how to use git worktree with Claude Code and other agents, a practical workflow for parallel agents, and the pitfalls to avoid.
What Is a Git Worktree?
Every Git repository has a working tree: the folder of files you edit, plus a hidden .git directory holding the history. Normally you have one working tree, so you can only have one branch checked out at a time. To work on something else, you stash or commit, switch branches, and switch back later.
git worktree lets one repository have several working trees at once. Each extra one (a "linked worktree") lives in its own folder and has its own checked-out branch, its own staged changes and its own HEAD. They all share the repository's objects, branches and remotes, so a commit made in one worktree is immediately visible from the others.
The feature has been in Git since version 2.5 (2015), so any modern Git installation has it.
What's shared and what isn't, according to the Git documentation:
| Shared across worktrees | Separate per worktree | |
|---|---|---|
| Commits and objects | Yes | |
Branches and tags (refs/) | Yes | |
| Remotes and most config | Yes | |
Checked-out branch (HEAD) | Yes | |
| Staging area (index) | Yes | |
| Files on disk | Yes | |
Untracked and ignored files (.env, node_modules) | Yes, each worktree starts without them |
Git Worktree vs Branch
Git worktree vs branch is a common point of confusion, because they work together rather than competing:
- A branch is a pointer to a line of commits. It's a concept inside the repository.
- A worktree is a folder on disk where a branch is checked out.
Without worktrees, you have one folder and switch it between branches. With worktrees, each branch you're actively working on can have its own folder. One rule: Git won't let the same branch be checked out in two worktrees at once (unless you force it), because two folders editing one branch would conflict.
Compared with other options:
| Switch branches | Stash | Second clone | Worktree | |
|---|---|---|---|---|
| Work on two branches at the same time | No | No | Yes | Yes |
| Disk space | Lowest | Lowest | Full copy of history | Only the extra files |
| Commits visible everywhere instantly | Yes | Yes | No, needs push and fetch | Yes |
| Setup per task | None | None | Clone, configure | One command |
| Good for parallel AI agents | No | No | Works, but clunky | Yes |
When to Use Git Worktree
Worktrees are worth it when you need more than one checkout at the same time:
- Running several AI coding agents in parallel, each on its own task.
- Reviewing a pull request without disturbing your current work.
- Hotfixes: fix production on a branch from
mainwhile a long feature is half-done. - Long builds or test runs on one branch while you keep coding on another.
- Comparing behaviour of two versions side by side.
They're less useful for tiny repos where a branch switch takes a second, and for projects whose setup (dependencies, databases, build caches) is very expensive to repeat per folder.
Git Worktree Commands
The main commands, from the Git documentation:
# Create a worktree in ../myapp-auth on a new branch called feature-auth
git worktree add ../myapp-auth -b feature-auth
# Create a worktree for an existing branch
git worktree add ../myapp-bugfix fix-login-bug
# Create a worktree at a specific commit, without a branch (detached HEAD)
git worktree add --detach ../myapp-test v2.3.0
# List all worktrees with their paths and branches
git worktree list
# Remove a worktree when you're done (refuses if it has uncommitted changes)
git worktree remove ../myapp-auth
# Force-remove a worktree with uncommitted changes
git worktree remove --force ../myapp-auth
# Clean up records of worktrees whose folders were deleted manually
git worktree pruneA few details worth knowing:
- `git worktree add` with
-bcreates a new branch; without it, it checks out an existing branch. It refuses if that branch is already checked out in another worktree. - `git worktree remove` refuses to delete a worktree with uncommitted or untracked changes unless you use
--force, which protects work you haven't saved. - `git worktree lock` stops a worktree from being pruned or removed, useful for worktrees on removable drives or ones a tool is still using.
- `git worktree list --porcelain` gives stable, script-friendly output.
- Deleting a worktree folder by hand leaves a stale record;
git worktree prunecleans it up.
Removing a worktree doesn't delete its branch or commits. Merge or delete the branch separately when you're finished.
Git Worktree With Claude Code
Git worktree Claude Code support is built in. Claude Code's --worktree flag (short form -w) creates a worktree and starts a session inside it:
claude --worktree feature-authAccording to Claude Code's documentation:
- The worktree is created under
.claude/worktrees/<name>/in your repository, on a new branch namedworktree-<name>. - Running the command again with a different name in another terminal starts a second, isolated session. If you leave out the name, Claude Code generates one.
- New worktrees branch from your repository's default branch by default; a
worktree.baseRefsetting of"head"makes them branch from your current work instead. - When you exit, Claude Code checks for changes: a clean worktree can be removed automatically, and one with changes prompts you to keep or remove it.
- A
.worktreeincludefile in your project root (using.gitignoresyntax) lists gitignored files, such as.env, to copy into every new worktree. - Subagents can also run in their own worktrees, for example with
isolation: worktreein a custom subagent's settings.
The docs suggest adding .claude/worktrees/ to .gitignore so worktree contents don't show up as untracked files in your main checkout.
You can also create worktrees yourself with git worktree add, cd into one and run claude there. That's the way to go when you want a specific existing branch or a different folder location. Our guide on how to use Claude Code covers the basics of sessions and permissions.
Other agents work with worktrees too: any coding agent that runs in a folder (Codex CLI, Gemini CLI, Cursor's agent, Aider and others) can be started in a worktree folder. Some editors and agent apps now create worktrees for parallel agents automatically.
A Workflow for Parallel AI Agents
Here's a practical routine for running three agents on one repository:
- Commit or stash your current work so new worktrees start from a known state.
- Split the work into independent tasks. Good candidates touch different parts of the code: a new API endpoint, a UI change, a test-coverage task. Tasks that edit the same files will conflict when you merge.
- Create one worktree per task:
git worktree add ../app-endpoint -b feat/export-endpoint
git worktree add ../app-ui -b feat/export-button
git worktree add ../app-tests -b chore/invoice-testsOr use claude --worktree <name> three times in three terminals.
- Set up each environment: install dependencies and copy local config. A fresh worktree has no
node_modules, virtual environment or.env. - Give each agent a clear, scoped prompt and the same project rules, for example in an AGENTS.md or CLAUDE.md file that's committed to the repo, so every worktree has it.
- Review each branch separately as a normal pull request: read the diff, run the tests.
- Merge one at a time, rebasing the others on the updated main branch if needed.
- Clean up:
git worktree removeeach folder and delete merged branches.
For bigger features, write the plan first. A short spec per task makes parallel agents far more reliable; see our guide to spec-driven development.
Running Parallel Agents With tmux
Many developers run each agent in its own terminal pane with tmux, so they can watch every session from one screen:
tmux new-session -d -s agents -c ../app-endpoint 'claude'
tmux split-window -h -t agents -c ../app-ui 'claude'
tmux split-window -v -t agents -c ../app-tests 'claude'
tmux attach -t agentsEach pane starts in a different worktree folder, so the agents never edit the same files. Switch panes to answer questions or approve actions, and detach with Ctrl-b d to leave them running. Any terminal multiplexer or a few terminal tabs work just as well; the important part is one agent per worktree.
Opening Worktrees in Your Editor
Because each worktree is just a folder, you can open it like any project:
- Separate editor windows: open each worktree folder in its own window, so file trees, terminals and Git views don't get mixed up.
- Language servers and indexes treat each worktree as its own project, which uses more memory but keeps things isolated.
- Avoid nesting worktrees inside the main folder unless that folder is ignored, or your editor and search tools will index every worktree twice.
- Git views in most editors show the branch for the folder you opened, so you always know which worktree you're in.
A Tidy Layout: Bare Repository Plus Worktrees
If you use worktrees every day, a common layout is to keep the repository itself as a "bare" clone (history only, no files) and put every branch, including main, in a worktree next to it:
git clone --bare [email protected]:acme/app.git app/.bare
cd app
echo "gitdir: ./.bare" > .git
git worktree add main main
git worktree add feature-auth -b feature-authYou end up with app/main/, app/feature-auth/ and so on, all as equal siblings. It keeps things orderly, but it's optional; adding worktrees next to a normal clone works fine. If you use this layout, check that git fetch updates remote branches; some setups need the remote's fetch rule set with git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*".
Merging the Results Back
Parallel work only pays off if merging is painless:
- Push each branch and open a pull request, or review locally with
git diff main...feature-auth. - Merge the smallest, most independent change first.
- Update the other branches with
git rebase main(or a merge) inside their worktrees, and rerun tests. - Resolve conflicts in the worktree that owns the branch, ideally with the agent that wrote the change, since it has the context.
- Delete merged branches and remove their worktrees.
If two agents keep conflicting, the tasks weren't independent enough. Split future work along clearer boundaries, such as separate modules or layers.
How Many Agents Should You Run?
More parallel agents isn't automatically better. Limits to consider:
- Your review capacity. Every agent produces a diff you need to read. Two or three parallel tasks is a common sweet spot for one developer.
- Machine resources. Each worktree may run its own dev server, test suite and build, which uses CPU, memory and disk.
- Task independence. If tasks overlap, you spend the saved time resolving merge conflicts.
- API usage limits and cost. Each agent uses its own model calls.
A practical rule: run as many agents as you can review properly, on tasks that don't touch the same files.
Handling Ports, Databases and Dependencies
Worktrees isolate files, not running services. Common problems and fixes:
| Problem | Fix |
|---|---|
| Two dev servers try to use the same port | Give each worktree its own port, for example through a .env value per worktree |
| Agents share one local database and step on each other's data | Use a separate database or schema per worktree, or run tests against throwaway databases |
| Missing dependencies in a new worktree | Install per worktree, or use a package manager with a shared cache to save disk and time |
Missing .env and local config | Copy them on creation; Claude Code's .worktreeinclude does this automatically |
| Docker containers with fixed names collide | Use a different Compose project name per worktree |
| Huge disk use from build outputs | Remove finished worktrees promptly and clear their build folders |
Git Worktree Best Practices
- Keep worktrees outside the main folder, such as
../app-feature, or in an ignored folder like.claude/worktrees/, so tools don't index them twice. - Name worktrees after their task so
git worktree listis easy to read. - One task, one branch, one worktree. Don't let two agents share a branch.
- Commit often in each worktree, so work is saved even if a session ends unexpectedly.
- Remove worktrees when done. Stale worktrees keep their branches checked out and block you from checking them out elsewhere.
- Avoid repos with submodules for heavy worktree use. Git's documentation says multiple checkouts are still experimental and support for submodules is incomplete.
Why Not Run Several Agents in One Folder?
It's tempting to start two agents in the same checkout. It usually goes wrong:
- Edits collide: both agents change the same files, overwrite each other, or read half-finished changes.
- Tests become unreliable: one agent's broken work-in-progress makes the other's tests fail.
- Git state gets confusing: both share one staging area and one branch, so commits mix unrelated changes.
- Review is harder: you can't tell which agent made which change.
Worktrees solve all four with almost no setup, which is why agent tools have adopted them.
Common Git Worktree Errors
- "fatal: '<branch>' is already checked out at '<path>'": that branch is open in another worktree. Use a new branch, or remove the other worktree first.
- "fatal: '<path>' already exists": the target folder exists. Choose another path or delete the folder.
- Remove refuses because of changes: commit or stash the changes, or use
git worktree remove --forceif you really want to discard them. - A worktree you deleted by hand still appears in the list: run
git worktree prune. - "is locked": run
git worktree unlock <path>first, if you're sure nothing is using it.
Git Worktree vs Clone, Stash and Submodule
People search for these comparisons because the names sound similar:
- Worktree vs clone: a clone copies the whole repository, history included. Worktrees share one history, use less disk, and see each other's commits instantly without pushing.
- Worktree vs stash: stash saves uncommitted changes so you can switch branches in one folder. A worktree avoids switching altogether.
- Worktree vs checkout: checkout switches the branch in your current folder; a worktree adds another folder with another branch.
- Worktree vs submodule: unrelated. A submodule embeds a different repository inside yours.
FAQ
What is a git worktree used for?
A git worktree lets you check out several branches of the same repository at once, each in its own folder. It's used for working on a hotfix while a feature is in progress, reviewing pull requests without disturbing your work, and running several AI coding agents in parallel.
What is the difference between a git worktree and a branch?
A branch is a line of commits in the repository; a worktree is a folder where a branch is checked out. Worktrees let different branches be checked out in different folders at the same time, sharing one history.
Does git worktree copy the whole repository?
No. Worktrees share the repository's history and objects. Each worktree only adds its own checked-out files, plus anything you install or build there, such as dependencies.
How do I use git worktree with Claude Code?
Run claude --worktree <name> to create a worktree under .claude/worktrees/ and start Claude in it, or create one with git worktree add and run claude inside that folder. Use a different name for each parallel session.
How do I delete a git worktree?
Run git worktree remove <path>. Git refuses if there are uncommitted changes unless you add --force. Removing a worktree doesn't delete its branch; delete the branch separately if it's no longer needed.