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.

By OmniAI Editorial · Nov 20, 2026 · 14 min read

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 worktreesSeparate per worktree
Commits and objectsYes
Branches and tags (refs/)Yes
Remotes and most configYes
Checked-out branch (HEAD)Yes
Staging area (index)Yes
Files on diskYes
Untracked and ignored files (.env, node_modules)Yes, each worktree starts without them
Three grey and light window panels with ink lines, one with an orange line, each joined by an ink line to a single grey cylinder below.
Each worktree has its own files and branch, but they all share one repository history.

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 branchesStashSecond cloneWorktree
Work on two branches at the same timeNoNoYesYes
Disk spaceLowestLowestFull copy of historyOnly the extra files
Commits visible everywhere instantlyYesYesNo, needs push and fetchYes
Setup per taskNoneNoneClone, configureOne command
Good for parallel AI agentsNoNoWorks, but clunkyYes

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 main while 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 prune

A few details worth knowing:

  • `git worktree add` with -b creates 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 prune cleans 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-auth

According to Claude Code's documentation:

  • The worktree is created under .claude/worktrees/<name>/ in your repository, on a new branch named worktree-<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.baseRef setting 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 .worktreeinclude file in your project root (using .gitignore syntax) lists gitignored files, such as .env, to copy into every new worktree.
  • Subagents can also run in their own worktrees, for example with isolation: worktree in 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.

Three grey terminal windows side by side with ink prompts, each above a light progress bar partly filled, the middle bar filled in orange.
Three agents, three worktrees, three tasks running at the same time.

A Workflow for Parallel AI Agents

Here's a practical routine for running three agents on one repository:

  1. Commit or stash your current work so new worktrees start from a known state.
  2. 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.
  3. 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-tests

Or use claude --worktree <name> three times in three terminals.

  1. Set up each environment: install dependencies and copy local config. A fresh worktree has no node_modules, virtual environment or .env.
  2. 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.
  3. Review each branch separately as a normal pull request: read the diff, run the tests.
  4. Merge one at a time, rebasing the others on the updated main branch if needed.
  5. Clean up: git worktree remove each 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 agents

Each 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-auth

You 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:

  1. Push each branch and open a pull request, or review locally with git diff main...feature-auth.
  2. Merge the smallest, most independent change first.
  3. Update the other branches with git rebase main (or a merge) inside their worktrees, and rerun tests.
  4. Resolve conflicts in the worktree that owns the branch, ideally with the agent that wrote the change, since it has the context.
  5. 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:

ProblemFix
Two dev servers try to use the same portGive each worktree its own port, for example through a .env value per worktree
Agents share one local database and step on each other's dataUse a separate database or schema per worktree, or run tests against throwaway databases
Missing dependencies in a new worktreeInstall per worktree, or use a package manager with a shared cache to save disk and time
Missing .env and local configCopy them on creation; Claude Code's .worktreeinclude does this automatically
Docker containers with fixed names collideUse a different Compose project name per worktree
Huge disk use from build outputsRemove 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 list is 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 --force if 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.

All articles

Read next

Language

EnglishEnglish · en TürkçeTurkish · tr DeutschGerman · de العربيةArabic · ar

Account

Enter your email. We’ll send you a sign-in link and a 6-digit code. No password needed.

Sign in

Theme

Language

Show our email address

To keep spam out of our inbox, please confirm your own email first. We will send you a 6-digit code.

We send codes only to these providers: gmail.com, googlemail.com, outlook.com, hotmail.com, live.com, msn.com, proton.me, protonmail.com, pm.me, icloud.com, me.com, mac.com, yahoo.com, aol.com, gmx.de, gmx.net, web.de, t-online.de, yandex.com, omniai.blog