Beta
Start for free

ResourcesHow-to

How to split Claude Code sessions across git worktrees: 5 steps from setup to cleanup

Splitting Claude Code sessions across git worktrees lets you run several sessions in parallel in one repository without their file edits colliding. In Claude Code, claude --worktree <name> creates the worktree and branch and starts the session in it. Each session gets its own branch and working directory.

The repository, configuration, build cache, and more stay shared between worktrees, though, and trouble with parallel sessions can start there. This article covers why to split sessions across worktrees, five steps from creating a worktree to cleaning it up, and what a worktree isolates.

In this article

Why split Claude Code sessions across worktrees

Run two Claude Code sessions in the same working directory, and both edit the same files and their changes collide. The worktrees documentation describes splitting parallel sessions into separate git worktrees as the way to keep their changes from colliding1. The common workflows documentation describes the same pattern, with the example of building a feature in one terminal while fixing a bug in another2.

A git worktree gives one repository several working directories, each with its own branch checked out. According to the git manual, a linked worktree shares everything in the repository except per-worktree files such as HEAD and the index3. According to the Claude Code documentation, worktrees isolate file edits, while subagents split up work inside one session1.

  1. Anthropic, Claude Code documentation, "Run parallel sessions with worktrees" (2026; retrieved November 27, 2026). ↩↩
  2. Anthropic, Claude Code documentation, "Common workflows" (2026; retrieved November 27, 2026). ↩
  3. The Git project, git-worktree manual (last updated in 2.56.0; retrieved November 27, 2026). ↩

5 steps to split Claude Code sessions across worktrees

1. Create a worktree and a branch for each task

Create one worktree, on a new branch, for each task a session works on. In Claude Code, claude --worktree <name> (-w) creates a worktree at .claude/worktrees/<name>/ under the repository root by default and starts the session on a new branch named worktree-<name>1. By default it branches from the remote's default branch, not your current branch. To branch from your local work instead, set worktree.baseRef to "head". The documentation recommends adding .claude/worktrees/ to .gitignore.

With git, give the location and a new branch, as in git worktree add ../project-feature-a -b feature-a. By default, git refuses to create a new worktree for a branch another worktree already has checked out3.

In my repository, worktrees live under .worktrees/ inside the repository, and a tracked .gitignore ignores .worktrees/ (a commit from September 27, 2026).

2. Set up the environment in the new worktree

A new worktree is a fresh checkout, so start by installing dependencies. The documentation says to do development setup, such as installing dependencies, inside the worktree1. Files ignored by .gitignore, such as .env, aren't copied into a new worktree. For worktrees Claude Code creates, list patterns in .worktreeinclude at the project root, and only files that match a pattern and are ignored by .gitignore are copied. Worktrees created with git worktree add don't get this.

In my setup, on October 10, 2026, a session of mine pushed a docs-only change from a new worktree without node_modules, and the pre-push hook failed. Both the command the hook ran and the base of the hook's configuration were read from node_modules. The hook ran lint, type checks, and unit tests even for docs-only changes.

3. Check what worktrees share

Some settings you change inside a worktree affect other sessions too. Check these three before you change a setting inside a worktree.

  • Git config: By default, the repository's config file is shared across all worktrees. When you need per-worktree settings, turn on the extension with git config extensions.worktreeConfig true and write them with git config --worktree3.
  • What Claude Code shares: Worktrees and the main checkout share the repository's .git, plugins installed at project scope, and Bash "don't ask again" permissions. Permissions are shared except in a few cases, such as on Windows, where Claude Code doesn't use the repository root. When a worktree has no .claude/skills, .claude/agents, or .claude/commands, the main checkout's are used1. This holds whether you created the worktree with --worktree or git worktree add.
  • Hook paths: When Claude enters a worktree partway through a session, a hook's ${CLAUDE_PROJECT_DIR} stays at the project root where the session started, while the hook input's cwd points to the worktree. In a worktree session started from the desktop app, hooks also run in the main checkout's root and ${CLAUDE_PROJECT_DIR} points there. Either way, hooks that work on files in the worktree should read cwd.

4. Check that build outputs don't point to another worktree

Sharing a build cache between worktrees can restore outputs that contain another worktree's absolute path. If turbo.json doesn't set cacheDir, Turborepo automatically shares its local cache between the main worktree and linked worktrees4. The Turborepo documentation warns that, because artifacts are restored without rewriting their contents, an output containing its worktree's absolute path can be restored pointing to another checkout.

In my setup, this failure happened on October 5 and November 1, 2026. The shared cache restored a build output of the database package that contained another worktree's absolute path, and after that worktree was deleted, every database access for authentication returned 500. On November 6, the same kind of output, containing the path of a different deleted worktree, turned up in a build for system tests. It looked like a bug in the sign-in screen, but the cause was the cache. I fixed it by regenerating the output and rebuilding without the cache.

To avoid this, the Turborepo documentation recommends setting a relative cacheDir such as .turbo/cache, which stops sharing and gives each worktree its own cache4.

5. Clean up worktrees you're done with

Once a branch is merged or abandoned, remove its worktree. When you end an interactive session, Claude Code checks whether the worktree has work that removing it would lose: changes, untracked files, or new commits1. If the worktree is clean, Claude removes it and its branch automatically for an unnamed session, and asks first for a named one (a session name, not the worktree name). If there's work in it, Claude asks whether to keep or remove it. Non-interactive runs with -p have no exit prompt, and their worktrees aren't cleaned up.

Remove worktrees you created with git using git worktree remove. If you delete the directory without remove, clean up the administrative files left in the repository with git worktree prune3. Build outputs that still contain a removed worktree's path cause the failure in step 4.

  1. Anthropic, Claude Code documentation, "Run parallel sessions with worktrees" (2026; retrieved November 27, 2026). ↩↩↩↩
  2. The Git project, git-worktree manual (last updated in 2.56.0; retrieved November 27, 2026). ↩↩↩
  3. Vercel, Turborepo documentation, "Caching" and "Configuring turbo.json" (2026; retrieved November 27, 2026). ↩↩

What a worktree isolates

A worktree isolates the files in its working directory and per-worktree files such as HEAD and the index. Unless you change their settings, the test database and development server ports are the same for every worktree.

In my setup, on October 5, 2026, I shared a test database with another session, and its tests recreated the database. That caused failures such as missing tables and foreign key violations, all of which looked as if my own change had broken something. Even with separate worktrees, sessions that point to the same database or ports affect each other's work. Create a dedicated database for each session, or decide an order so they aren't used at the same time.

How Harnessforce can help

When you run sessions side by side in worktrees, the work moves forward branch by branch. With Harnessforce, everyone on the team can see how far the work you handed to AI has gotten and where it's stuck, so people can make the call and steer.

With Harnessforce, you can:

  • See progress by issue: For each issue, see how far the work you handed to AI has gotten, where it stopped, and how CI and review went.
  • See it on the issue: The issue page brings together linked pull requests and sessions, and their history.
  • Follow it to the conversation and traces: From an issue, follow the work to the session's conversation and its traces.
  • Link runs to pull requests: Link sessions to issues and pull requests, with CI and review results next to them. Inferred links are labeled as inferred.
  • Work alongside your tools: Keep using Claude Code and GitHub as they are; Harnessforce runs alongside them.

Learn more on the Read the history page, talk to us, or get started today.


This article was written with AI assistance and reviewed by the author.