iCode Workbench · Guide · Isolation

Worktrees vs branches vs clones

Branches, worktrees, and clones are often used as if they were interchangeable ways to “work on something else.” They are not. They isolate different things, cost different amounts, and fail in different ways when two coding agents run at once.

This guide compares the three for the specific case that matters here: multiple agents in one repository.

This guide is currently available in English only.

Definitions

BranchWorktreeClone
What it is A movable pointer to a commit A separate working directory attached to the same repository A separate repository with its own object store
Working directory Shared Separate Separate
Git history Shared Shared Duplicated
File isolation No Yes Yes
Setup cost Lowest Low High

A branch changes which commit you are looking at. A worktree changes where you are looking at it. A clone changes the repository itself.

Isolation level

A branch gives you no filesystem isolation. Two agents can check out different branches in the same directory and still overwrite each other's files, because they share the working tree. Switching branches under a running agent is worse: the files change while it is reading them.

A worktree gives you filesystem isolation with shared history. Each worktree has its own working directory, index, and HEAD, so two agents can work at the same time without seeing each other's edits. Git refuses to check out the same branch in two worktrees, which prevents the most obvious collision.

A clone gives you repository-level isolation. It has its own object store and remote configuration, so it is the right tool for a separate machine, a separate fork, or a completely separate line of work. It is heavier than a worktree and unnecessary for parallel agents in one repository.

Disk and setup cost

A branch is nearly free: it is a pointer. A worktree is cheap: it shares the object store, so creating one does not duplicate history. A clone duplicates the object store and may re-download the repository.

But the worktree is not free at the development-environment level. Every checkout needs its own dependencies, .env, ports, and database. Three worktrees can mean three node_modules installs and three dev servers. The Git cost is low; the environment cost is the real one.

Shared state

All three share more than the file list suggests. Git hooks, stash, and config are repository-level. Submodules need their own initialization. Credentials, SSH keys, and network access are machine-level. A worktree isolates the working tree; it does not isolate the machine.

If you need process, network, or database isolation, a worktree is not enough. Use a container per worktree, or a clone in a sandbox. The decision is about the threat model, not the number of agents.

Shared state checklist

Before you treat a worktree as isolated, check what it still shares:

  • Git hooks. Repository-level, not worktree-level. A pre-commit hook runs in every checkout.
  • Stash. Shared across worktrees. Do not stash in one and expect another not to see it.
  • Config. Local Git config is shared unless you override it per worktree.
  • Submodules. Need their own initialization in each checkout.
  • Env files. Gitignored files are not in a fresh worktree. Copy them.
  • Dependencies. node_modules, virtualenvs, and build caches are per-directory.
  • Ports. Two dev servers both want 3000. Assign one per worktree.
  • Databases and caches. A migration in one worktree can corrupt another's test data.
  • Credentials and network. A worktree is not a sandbox; it still uses your machine's keys and network.

If any of these matter, the worktree is not the whole isolation story. Add a setup script, a container, or both.

When to use each

  • Branch: switching context in one checkout, reviewing a change, or keeping a line of work without running it in parallel.
  • Worktree: two or more agents in one repository, each on an independent task; reviewing a diff in the checkout where it was made.
  • Clone: a separate machine, a separate object store, a fork, or a fully isolated environment where sharing history is undesirable.

A useful rule: if the agents are working in the same repository and on independent files, use worktrees. If they are working on the same files, they are one task; no isolation tool fixes that.

Common mistakes

  • Using a branch as file isolation. Two agents in one checkout with different branches still share the working tree.
  • Cloning for every agent. It works, but it duplicates history and setup cost. A worktree is cheaper for the same file isolation.
  • Creating a worktree without setup. A clean checkout has no .env, no dependencies, and no database. It is not a working environment yet.
  • Checking out the same branch twice. Git refuses, and forcing it recreates the collision the worktree was meant to prevent.
  • Removing a worktree with uncommitted changes. Removing it deletes the working tree. Merge or keep the branch first.

How to choose

Three questions decide it:

  1. Are two agents working at the same time? If yes, you need file isolation: a worktree or a clone.
  2. Do they need the same repository history? If yes, a worktree is the cheaper isolation. If no, a clone.
  3. Do they need process or network isolation? If yes, a worktree is not enough. Add a container or run the clone in a sandbox.

If the answer to the first question is no, a branch is enough.

Commands at a glance

CreateSwitchRemove
Branch git switch -c feature-auth git switch feature-auth git branch -d feature-auth
Worktree git worktree add ../project-auth -b feature-auth cd ../project-auth git worktree remove ../project-auth
Clone git clone <url> ../project-auth cd ../project-auth Delete the directory

The branch row changes what you are looking at; the worktree and clone rows change where you are looking at it. Only the last two give you a separate working tree.

Quick answers

Does a worktree duplicate the repository?

No. It shares the object store and history. Only the working directory and index are separate, which is why creating one is cheap.

Can two worktrees use the same branch?

No. Git refuses to check out the same branch in two worktrees. That is a safety feature: it prevents two agents from committing to the same branch from different directories.

Is a clone better for isolation?

It is more isolated because it has its own object store and remote configuration, but it is heavier. Use a clone for a separate machine, a fork, or a genuinely separate repository, not for every agent in one repo.

Can I delete a worktree safely?

Only after the branch is merged or the changes are committed. Removing a worktree deletes the working tree, including uncommitted changes.

Recommendation for agents

Use one worktree per task. It gives you file isolation at the lowest Git cost, keeps the main checkout clean, and makes the review local to the work. Use a clone only when you need a separate repository or machine. Use a branch alone only when you are the only agent in the checkout.

For the mechanics, read Git worktrees for coding agents, explained. For the full workflow, read the parallel-agents pillar.