Skip to content
Academy ofAI Learning

~/academy $ claude learn parallel-work-with-worktrees

Parallel sessions with git worktrees

Run several Claude Code sessions at once without them overwriting each other, using the --worktree flag, .worktreeinclude, subagent isolation and background tasks. 8 min

Watch it done: run two Claude Code sessions in separate worktrees

$

Step 1 of 7

Open a terminal in your git repository. Worktrees need git, and Claude Code creates them under .claude/worktrees at the repository root.

  1. Open a terminal in your git repository. Worktrees need git, and Claude Code creates them under .claude/worktrees at the repository root. Type: cd ~/projects/my-app.
  2. Add the worktrees folder to .gitignore, so worktree files do not show up as untracked files in your main checkout. Type: echo ".claude/worktrees/" >> .gitignore.
  3. A worktree is a fresh checkout, so ignored files such as .env are missing. List them in .worktreeinclude at the project root and Claude Code copies them into each new worktree. Create .worktreeinclude containing: .env .env.local
  4. Start the first session in its own worktree. It branches from your default branch on the remote, usually main. Type: claude --worktree feature-auth. Claude Code creates .claude/worktrees/feature-auth on a new branch called worktree-feature-auth and starts the session there. If you have never run claude in this folder, run it once first to accept the trust dialog.
  5. In a second terminal, in the same repository, start another session with a different name. The two sessions cannot overwrite each other. Type: claude --worktree bugfix-invoices. A second worktree on its own branch, worktree-bugfix-invoices, with its own copy of the files.
  6. When a session is finished, exit it. Claude Code checks for work before deleting anything. Type: /exit. A clean worktree is removed with its branch. If it has changes or new commits, Claude Code asks whether to keep or remove it; keep it and it prints a claude --worktree <name> --resume command for later.
  7. See what is left. You can always tidy up with git directly, for example git worktree remove followed by the path. Type: git worktree list. Git lists your main checkout and any worktrees you kept.

Commands from the official Claude Code worktrees and CLI reference docs, checked 18 November 2026. Lines marked Note describe what happens and are not real output.

Step 1 of 7

$ Read

Why parallel sessions collide

Two Claude Code sessions in the same folder edit the same files. One builds a feature while the other fixes a bug, and each trips over the other's half-finished changes.

A git worktree fixes this. It is a second working folder with its own files and its own branch, sharing the same repository history. Give each session its own worktree and they cannot overwrite each other.

Lesson notes: Parallel sessions with git worktrees

Everything in this lesson as text, for revision or to read without the interactive steps. By the end you will be able to: You run two Claude Code sessions side by side in separate worktrees and clean them up safely.

  1. Step 1 of 7 · Read

    Why parallel sessions collide

    Two Claude Code sessions in the same folder edit the same files. One builds a feature while the other fixes a bug, and each trips over the other's half-finished changes.

    A git worktree fixes this. It is a second working folder with its own files and its own branch, sharing the same repository history. Give each session its own worktree and they cannot overwrite each other.

  2. Step 2 of 7 · Try it in the terminal

    Start a session in its own worktree

    In your repository, start Claude Code in a new isolated worktree named feature-auth.

    Hint: claude, then --worktree (or -w), then the name.

    Show the command
    $ claude --worktree feature-auth
    Created worktree .claude/worktrees/feature-auth on branch worktree-feature-auth
    Claude Code
    cwd: ~/projects/booking-app/.claude/worktrees/feature-auth
    
    > 
  3. Step 3 of 7 · Read

    What --worktree sets up

    Claude Code creates the worktree at .claude/worktrees/<name>/ on a new branch called worktree-<name>. Leave out the name and it makes one up. Open another terminal and run it again with a different name for a second session. Add .claude/worktrees/ to your .gitignore.

    New worktrees branch from your default branch on the remote, usually main. Set worktree.baseRef to "head" in settings to branch from your current work instead. To start from a pull request, pass its number: claude --worktree "#1234".

    A worktree is a fresh checkout, so ignored files such as .env are missing and dependencies need installing. List the files to copy across in a .worktreeinclude file at the project root, written like a .gitignore.

    .worktreeinclude
    .env
    .env.local
    config/secrets.json
  4. Step 4 of 7 · Read

    Cleaning up

    When you exit, Claude Code checks for work. A clean worktree from an unnamed session is removed with its branch. If there are changes or new commits, it asks whether to keep or remove it. Keep it and it prints a claude --worktree <name> --resume command to come back later.

    Runs with -p have no exit prompt, so they leave their worktrees behind. You can always manage worktrees with git directly.

    Make one yourself
    git worktree add ../booking-bugfix -b bugfix-invoices
    See them all
    git worktree list
    Remove one
    git worktree remove ../booking-bugfix
  5. Step 5 of 7 · Read

    Other ways to work in parallel

    Subagents in worktrees: add isolation: worktree to a subagent's frontmatter so each copy edits its own checkout. The bundled /batch skill splits one large change into 5 to 30 worktree-isolated subagents and shows you the plan first.

    Background commands: press Ctrl+B to push a long-running command, such as a dev server or test watcher, into the background and keep talking to Claude. /tasks lists everything running in the background of the session.

    Background sessions: claude --bg "investigate the flaky test" starts a whole session in the background, and claude agents opens agent view to watch them all (a research preview, checked 18 November 2026). Every parallel session uses its own tokens, so usage climbs with each one.

  6. Step 6 of 7 · Quiz

    Check your understanding

    You start a worktree session and the app will not run because the .env file is missing. Best fix for next time?

    • Commit .env to git so every checkout has it
    • Add .env to a .worktreeinclude file at the project root
    • Never use worktrees for apps that need settings
    • Set worktree.baseRef to "head"
    Show the answer

    Add .env to a .worktreeinclude file at the project root Claude Code copies matching gitignored files into each new worktree it creates.

  7. Step 7 of 7 · Sort

    Worktree session or background command?

    Pick the better tool for each job.

    • Build a new feature while another session fixes a bug
    • Keep the dev server running while you keep chatting
    • Try two competing designs for the same component
    • Leave a test watcher running and check its output later
    • Review a teammate's pull request without touching your work
    Show the answers
    • Build a new feature while another session fixes a bug: Separate worktree session. Both edit files. Separate checkouts stop them colliding.
    • Keep the dev server running while you keep chatting: Background command (Ctrl+B). One long-running command, no separate edits needed.
    • Try two competing designs for the same component: Separate worktree session. Each attempt needs its own copy of the files and its own branch.
    • Leave a test watcher running and check its output later: Background command (Ctrl+B). A background task keeps going and Claude can read its output.
    • Review a teammate's pull request without touching your work: Separate worktree session. claude --worktree "#1234" checks the PR out in its own folder.