Skip to content

Day 24 of 52 · about 15 min · Subagents

Run two Claude sessions without collisions

Two sessions in one folder edit the same files, so one’s change lands on top of the other’s. A worktree gives each session its own checkout and branch, so a fix and a feature run side by side.

You’ll finish with

  • Two Claude Code sessions on separate branches of one repository
  • A .worktreeinclude that copies your ignored .env into each new worktree
  • Worktrees removed or kept on purpose when you exit

You’ll need

  • A git repository with at least one commit. Run claude in it once and accept the trust prompt
  • Two terminal windows open in the project folder

Before you start

Tell us about your setup and the steps change to fit it. Your answers stay in this browser, and you can change them at any time.

Which system are you on?

Commands differ between systems. Pick the one you’ll run Claude Code on.

The steps

One step at a time. When a step’s check matches, tick it and the next one opens.

0 of 4 steps done0%
  1. Step 1 of 4: Keep worktrees out of git status

    Worktrees live inside your project, so tell git to ignore that folder.

    Add one line to .gitignore, then commit it.

    Changes to .gitignore

    Unchanged: node_modules
    Unchanged: .env
    Added: .claude/worktrees/
    The practice project’s .gitignore with the new line.

    Check it worked

    After you commit, git status no longer lists .gitignore.

  2. Step 2 of 4: Copy your .env into new worktrees

    A worktree only gets tracked files, so list the ignored ones it should copy.

    Create .worktreeinclude in the project root with the line .env. It uses .gitignore patterns and copies only gitignored files. In the practice project, first save a copy of .env.example as .env.

    your-project/

    • .envignored by git, copied into each worktree
    • .gitignoreChanged
    • .worktreeincludeNewone line: .env
    • folder src
    Tracked files come with every worktree. .worktreeinclude adds the ignored ones you name.

    Check it worked

    git status does not list .env, because it is ignored.

  3. Step 3 of 4: Start two sessions in their own worktrees

    Give each session a name; each gets its own folder and branch.
    Terminal

    Run: claude --worktree fix-rounding

    What happens: In the second terminal, same folder, run it again with another name, such as add-rush-fee.

    The same command works in every terminal.

    Give each session a different task. A worktree is a fresh checkout, so in your own project install dependencies there first.

    Check it worked

    In a third terminal, git worktree list shows your main folder plus two under .claude/worktrees/, on branches worktree-fix-rounding and worktree-add-rush-fee. Each worktree has a .env.

    If it didn’t work

    It fails with "Failed to resolve base branch"

    The repository has no commits yet. Commit once, then try again.

  4. Step 4 of 4: Exit and clean up

    When you exit, Claude Code checks each worktree before removing it.

    In order:

    1. You

      You exit the session

    2. Claude

      Claude Code checks the worktree

      Changed or untracked files, new commits

    • If it is clean

      Removed with its branch

      It asks first if you named the session

    • If it has work in it

      You choose

      Keep it, or remove it and the work

    Nothing with work in it is deleted without asking.

    Check it worked

    git worktree list shows only the worktrees you kept. To return, run the claude --worktree <name> --resume command printed on exit.

Finish · 4 of 4 steps to go

Two sessions, two branches, no collisions

Merge a worktree’s branch like any branch. New worktrees start from your remote’s default branch, or your current commit if there is no remote.

What you’ll have checked

  • .claude/worktrees/ is ignored
  • Each worktree got its own .env
  • Only kept worktrees remain

Lesson updates by email

Get lesson updates by email

Leave your email and we’ll tell you when we add or update a lesson.

We’ll email you when we add or update a lesson. We use your email only for this. Privacy policy

Claude Code for your team

Want Claude Code set up across your team?

HorizonLux sets up and repairs Claude Code for teams: CLAUDE.md rules, hooks that enforce them and reviewed MCP servers. Book a free call to talk it through.

Questions

Questions about Day 24

How do I remove a worktree Claude Code left behind?

Runs with `-p` have no exit prompt. Run `git worktree remove <path>` (from `git worktree list`), after `git worktree unlock <path>` if it’s locked.

Sources

Checked against the official Claude Code docs on .

Independent resource, not affiliated with Anthropic. HorizonLux wrote this lesson; Claude Code is made by Anthropic.