Lesson 7 of 9 · about 15 minutes

Parallel sessions with worktrees

One repository, several folders, one branch each, so two agents never trip over each other.

You will run two Claude Code sessions at the same time on two branches, one started with claude -w and one with git worktree add, and clean both up afterwards.

Why one folder isn't enough

Your penguins-lab folder is a single working tree, and it can have only one branch checked out at a time. Say one Claude session is working on add-species-plot and you git switch the folder to another branch for a second task. Now the first session's files change underneath it. If you skip the switch and run both sessions in the same folder, they edit the same files on the same branch. Either way the work collides.

The fix is a worktree: an extra folder attached to the same repository, with its own branch checked out. Git calls this managing "multiple working trees attached to the same repository." All worktrees share commits and branches, and each has its own HEAD and index.1 Claude Code's best-practices guide recommends worktrees for exactly this: "run separate CLI sessions in isolated git checkouts so edits don't collide."3

one repository commits + branches penguins-lab/ branch main · you ../penguins-lab-plot/ branch add-species-plot · Claude #1 .claude/worktrees/add-summary-stats/ worktree-add-summary-stats · Claude #2
Three folders, three branches, one shared history. A commit made in any folder is visible to the others straight away, because there is only one repository.

Plain git: git worktree

$ git worktree add ../penguins-lab-plot -b add-species-plot
Preparing worktree (new branch 'add-species-plot')
$ git worktree list
…/penguins-lab       a7004ec [main]
…/penguins-lab-plot  a7004ec [add-species-plot]
$ cd ../penguins-lab-plot
$ claude                        # this session lives on add-species-plot

add <path> -b <branch> creates the folder and a new branch in one step. list shows the main worktree first, then the linked ones, each with its branch.1 When you're done, git worktree remove ../penguins-lab-plot deletes the folder. "Only clean worktrees … can be removed" unless you add --force, and the main worktree can never be removed.1 Removing a worktree keeps its branch, so delete the branch separately.

A branch can be checked out in only one worktree at a time.1 If you try git switch add-species-plot in your main folder while the worktree has it, Git stops you: fatal: 'add-species-plot' is already used by worktree at '…/penguins-lab-plot'. That isn't a bug. It's the guarantee that keeps two sessions apart.

The Claude Code shortcut: claude -w

Claude Code can create the worktree for you. claude -w add-summary-stats (long form --worktree) makes the folder under .claude/worktrees/add-summary-stats/ "on a new branch named worktree-<name>", here worktree-add-summary-stats, and starts Claude inside it.2 Things to know:

Data-science gotcha: every worktree needs its own environment

A worktree is a fresh checkout, so install dependencies there.2 Your .venv folder is gitignored, and virtual environments "should not be checked into source control" and are "disposable," so you recreate one rather than copying it.5 In each new worktree:

$ python -m venv .venv
$ source .venv/bin/activate        # Windows PowerShell: .venv\Scripts\Activate.ps1
$ pip install -r requirements.txt

Gitignored files such as .env (API keys, database URLs) aren't copied into a new worktree either. List them in a .worktreeinclude file (gitignore syntax) and claude -w copies them in for you.2

From two worktrees to two pull requests

Each worktree's branch is an ordinary feature branch. Push it, open its own pull request, and CI checks each one separately (lessons 4–5). Merge whichever finishes first. The second pull request may now be behind main. GitHub recommends merging the base branch into your topic branch frequently,6 so ask that session's Claude to merge main in and rerun pytest.

Cleanup after a squash merge: the branch's original commits never land on main, only the squashed copy does. So git branch -d add-species-plot refuses ("not fully merged"). Once you've confirmed the pull request merged on GitHub, git branch -D is safe.7

Do it for real

  1. Lesson 11 explains why this step matters.

Check yourself

Why not run two Claude sessions on different branches in the same folder?

A working tree has exactly one branch checked out. Switching it pulls the files out from under the other session. Worktrees give each session its own folder.

Which branch does claude -w add-summary-stats create?

The worktree goes in .claude/worktrees/add-summary-stats/ on a new branch named worktree-<name>.

add-species-plot is checked out in a worktree. In your main folder you run git switch add-species-plot. What happens?

A branch can be checked out in only one worktree at a time. Git says it "is already used by worktree at …".

In a brand-new worktree, pytest fails with ModuleNotFoundError: pandas. Most likely cause?

A worktree is a fresh checkout and your gitignored .venv isn't in it. Create a venv and pip install -r requirements.txt there.

Command drills

Create a worktree at ../penguins-lab-plot on a new branch add-species-plot.

Show every worktree attached to this repository and its branch.

Remove the (clean) worktree at ../penguins-lab-plot.

Start Claude Code in a new isolated worktree named add-summary-stats.

Read Run parallel sessions with worktrees in the Claude Code docs. It covers base branches, .worktreeinclude, cleanup, and subagents with isolation: worktree (about 10 minutes). For the underlying git, see git-worktree.
Ask Claude: "Which of my worktrees have unpushed commits?" or "I want to split this feature into two parallel tasks. Suggest the branches and worktrees." Treat it as a planning partner, not just a coder.

Sources

  1. Git reference, git-worktree (add, list, remove; one branch per worktree). Behaviour and messages also verified locally with Git 2.52.
  2. Claude Code docs, Run parallel sessions with worktrees (start, base branch, .worktreeinclude, cleanup, isolation).
  3. Claude Code docs, Best practices — Run multiple Claude sessions.
  4. Claude Code docs, Common workflows — Run parallel sessions with worktrees; CLI reference (--worktree, -w).
  5. Python docs, venv — Creation of virtual environments.
  6. GitHub Docs, Branches — Merging often.
  7. Git reference, git-branch (-d requires fully merged; -D forces).