- Tools
- Claude Code, git
- Price
- Included (Claude plan or API)
- Time
- 10 min
- Level
- Terminal, intermediate
You ask Claude to fix the contact form on a website. While it works, you open a second terminal and start another session to build the pricing page. Both edit the same folder, sometimes the same file, and one undoes what the other just wrote. The problem is not the AI: two people typing in the same document at once would make the same mess. The fix is called a worktree.
What a worktree is for
A git worktree is a second working folder attached to the same project: it has its own files and its own branch, but shares the history and the remote. Each Claude Code session works in its own, like each colleague on their own computer. It helps a freelancer or a small team who wants to hand two or three tasks to AI agents at once: a bug fix, a new page, a translation update.
Start two sessions in parallel
- Check the project is a git repository with at least one commitClaude Code creates the worktree from an existing commit. In an empty repository, the command fails.
git log --oneline -1 - Ignore the worktrees folderClaude Code puts them in
.claude/worktrees/at the project root. The documentation recommends adding it to.gitignoreso their files do not show up in your main checkout.echo ".claude/worktrees/" >> .gitignore - First sessionThe name you choose becomes the folder
.claude/worktrees/contact-form/and the branchworktree-contact-form. The first time in a folder, just runclaudeto accept the folder trust prompt.claude --worktree contact-form - Second session, in another tab
-wis the short form of--worktree.claude -w pricing-page
Prepare each copy
A worktree is a fresh checkout: dependencies and git-ignored files such as .env are not there. Two settings save you from redoing everything by hand.
- Dependencies: ask Claude, at the start of the session, to install them (
npm installor your project's equivalent). - Local files: create a
.worktreeincludefile at the root. It uses.gitignoresyntax, and only files that are both listed and ignored by git are copied into each new worktree.
.env
.env.localAn example in an agency
Monday morning, three requests from a client about their website. I start three sessions, each with a closed instruction:
You are working alone on this task, in this worktree. Task: fix the contact form submission (the confirmation message does not show). Only touch the files you need. Run the build and the tests before finishing. End with a commit and a three-line summary: what changes, what was tested, what was not.The other two sessions get the same frame with their own task. I then review each branch and open a pull request for it, one per topic.
When the session ends
- Nothing changed: Claude Code removes the worktree and its branch (it asks first if the session has a name).
- There is work left: it asks whether to keep or remove it. Keeping preserves the folder and branch, and Claude prints the command to resume later.
- By hand:
git worktree listshows the copies,git worktree removedeletes one.
Subagents can also each work in a worktree: ask Claude to "use worktrees for your agents", or add isolation: worktree to the frontmatter of a custom agent.
Limits and cost
- Conflicts do not disappear, they move: if two branches change the same lines, git will tell you at merge time. Give each session a task that touches different files.
- Each session uses its own tokens: every agent has its own conversation, which Claude rereads with each message. Several agents in parallel raise usage accordingly; on a subscription, you will reach the limit sooner.
- The starting point: by default, the worktree branches from the remote's default branch, not from your work in progress. Setting
worktree.baseRefto"head"changes that. - Disk space: each copy takes room, especially with its dependencies. Clean up regularly.
Security. .worktreeinclude copies your secrets files into every worktree. Check that .claude/worktrees/ is ignored by git (git check-ignore .claude/worktrees/) before committing anything, and only list what the agent really needs.
Sources
- Claude Code: Run parallel sessions with worktrees
- Claude Code: Common workflows
- Claude Code: Manage costs effectively
- git worktree documentation
Want a custom tool built with this method, branch by branch and reviewed? That is what I do in custom software.