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
.worktreeincludethat copies your ignored.envinto each new worktree - Worktrees removed or kept on purpose when you exit
You’ll need
- A git repository with at least one commit. Run
claudein 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.
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_modulesUnchanged: .envAdded: .claude/worktrees/The practice project’s .gitignore with the new line. Check it worked
After you commit,
git statusno longer lists.gitignore.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
.worktreeincludein 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.exampleas.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 statusdoes not list.env, because it is ignored.Step 3 of 4: Start two sessions in their own worktrees
Give each session a name; each gets its own folder and branch.TerminalRun: 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 listshows your main folder plus two under.claude/worktrees/, on branchesworktree-fix-roundingandworktree-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.
Step 4 of 4: Exit and clean up
When you exit, Claude Code checks each worktree before removing it.In order:
You
You exit the session
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 listshows only the worktrees you kept. To return, run theclaude --worktree <name> --resumecommand printed on exit.
Finish · 4 of 4 steps to go
Two sessions, two branches, no collisions
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.
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.
Related
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.