Founders BetaFounder price: US$ 9.90/month locked for as long as you subscribe.→
HomeBlogDicasRunning Claude Code in parallel without conflicts: a worktrees guide
Claude Code documentation card about parallel sessions with worktrees
Image: Anthropic (Claude Code Docs)
Dicas6 min

Running Claude Code in parallel without conflicts: a worktrees guide

Run two Claude Code sessions at the same time without file conflicts using git worktree and the --worktree flag. Step-by-step verified against the official docs.

You ask Claude Code for a feature and, while it works, an urgent bug appears. Opening another terminal in the same directory seems quick, but both sessions start editing the same files.

The biggest AI newsletter in the World

Get AI articles in your inbox

Technical curation on models, agents and coding tools. No spam, unsubscribe anytime.

Daily at 8:00 and 16:00 Brasília time. No spam.

The result is mixed changes, tests breaking for no clear reason, and a commit that is hard to separate. The solution indicated by the official Claude Code worktrees documentation is to give each session its own copy of the files.

In this article you will see:

  • what a git worktree is and why it avoids conflict
  • how to open two parallel sessions with a flag
  • how to prepare the environment and bring the .env
  • how to clean up and resume a session

What is a git worktree

A git worktree is a separate working directory, with its own files and branch, that shares the history and remote of the main repository. It is different from cloning the project again.

Running each Claude Code session in a worktree lets one session build the feature while another fixes the bug without editing the same files. The worktree starts from an existing commit, so the repository needs at least one commit.

Step 1: open the first session with --worktree

Use the flag --worktree (or -w) with a name. Claude Code creates the worktree in .claude/worktrees//, at the repository root, on a new branch called worktree-:

Terminal
claude --worktree feature-auth

If you omit the name, Claude generates one, such as bright-running-fox. In interactive runs, you must first accept the directory trust dialog. If you have never run Claude there, run claude once in the project.

Step 2: open the second session in another terminal

Repeat the command with another name in a second terminal:

Terminal
claude --worktree corrige-bug-login

Done: two isolated sessions, each on its own branch. You can follow one while the other works and review each result separately afterward.

Step 3: ignore the worktrees folder in git

Add .claude/worktrees/ to .gitignore. The documentation recommends this so that worktree contents do not appear as untracked files in your main checkout.

gitignore · .gitignore
.claude/worktrees/

Step 4: prepare the environment and bring the .env

A worktree is a fresh checkout, so dependencies do not come along. Ask Claude to install everything or run the project setup inside the worktree folder.

Git-ignored files, such as .env, are not copied either. To bring them to each new worktree, the documentation indicates creating a .worktreeinclude file. The exact format is in the corresponding section of the official page.

Step 5: understand what Claude blocks

When the session is isolated, Claude Code blocks edits to Edit, Write, and NotebookEdit that point to the main checkout. It also blocks commands whose working directory resolves to the main checkout and git redirects to it.

There is a limit: the documentation warns that commands like cp or shell redirection writing to the main checkout are not blocked by these checks. They follow your normal permission and sandbox rules.

Step 6: exit, clean up, and resume

When exiting a session in a worktree, Claude checks whether there is work in it. If it is clean and the session has no name, the worktree and branch are removed. If there are changes, it asks whether you want to keep or remove them.

Caution: removing deletes the directory and the branch with all the work inside. Ignored files, such as .env, are not counted and can be lost on exit, so copy what you need before leaving.

To return to a kept worktree, run from the main checkout with the session ID shown on exit:

Terminal
claude --worktree feature-auth --resume <id-da-sessao>

Other ways to enter a worktree

You can also ask, in the middle of a session, for Claude to work in a worktree. In that case it uses the EnterWorktree tool and creates the space for you, without needing to restart the session.

For background sessions, the documentation mentions the claude --bg --worktree combination, which keeps edits out of the main checkout. If you already use this setup, it's worth checking the isolation section of the official page.

When to use subagents instead of sessions

Worktrees isolate edits between sessions. Subagents, on the other hand, divide work within a session. You can combine both with isolation: worktree in the subagent file, as shown in the guide for creating subagents in Claude Code.

If your case involves two different tools, see Claude Code and Codex together in the same repository without conflict. To control what Codex can write, there's a post about the Codex CLI sandbox and approvals.

Frequently asked questions about parallel Claude Code sessions with worktrees

How to run two Claude Code sessions in parallel?

Use claude --worktree <name> in each terminal, with different names. Each session gets its own worktree and branch, so edits don't collide.

Where does Claude Code create the worktrees?

In .claude/worktrees/<name>/ at the repository root, on a branch called worktree-<name>. Add .claude/worktrees/ to .gitignore.

Is .env copied to the worktree?

Not automatically. A worktree is a fresh checkout, so git-ignored files are left out. To bring them along, the docs point to a .worktreeinclude file.

What happens to the worktree when you exit the session?

If the worktree is clean and the session wasn't named, it and its branch are removed. If there's work, Claude asks whether you want to keep or remove it.

Does the worktree eliminate all git conflicts?

Not all of them. It avoids file conflicts during work, but if two branches change the same section, the conflict shows up at merge time.

How to run agents in parallel without conflict

The minimum that works is simple: one name per task, .claude/worktrees/ in the .gitignore, and dependencies installed in each worktree. Then, review and integrate each branch one at a time.

The worktree prevents file conflicts during work, but it doesn't decide the integration order for you. If two branches touch the same section, the conflict shows up in the merge, as in any git workflow.

If you want to track multiple agents and their isolated spaces in one place, see how orchestration works in bridgecode.

Source: Claude Code Docs

Share this article

Tags:#claude-code#git-worktree#agentes em paralelo#dicas
Marcelo Santiago
Marcelo Santiago·Creator of bridgecode

Developer and creator of bridgecode, an agentic development environment for orchestrating squads of AI agents. Helps people going from vibe coding to professional development move from idea to shipped product.

The biggest AI newsletter in the World

Enjoyed this content? Stay ahead in AI engineering.

Code analysis, multi-agent architecture, token benchmarks and tools selected by developers for developers.

Daily at 8:00 and 16:00 Brasília time. No spam.