Isolate two code tasks with Claude Code worktrees
Use claude --worktree to start one isolated git worktree session for a feature and another for a bug fix, so two terminals do not overwrite each other's edits.
Before you start · 1. Confirm the repository state · 2. Start the feature task in terminal one
At a glance
When a new feature and a bug fix are in progress at the same time, two Claude Code sessions editing the same checkout mix their changes, which makes them hard to review and commit separately. The official documentation uses git worktrees: each worktree is a separate checkout on its own branch. This guide starts two isolated sessions in two terminals, finishes one job in each, and inspects each diff separately. The example project, shop-demo, is fictional and is not a real run result.
Workflow
Commit first
Confirm the repository has at least one commit so a worktree can be created.
One name, one session
Start one worktree session per task, each with a different name.
Check each diff
Inspect changes and tests in each worktree before deciding on cleanup.
Before you start
- Claude Code starts in your terminal, the project is a git repository, and it has at least one commit (the documentation says a worktree is created from an existing commit; installation and login were not verified here).
- The two tasks are written down separately, for example one feature and one bug fix, ideally touching different code.
- You have two terminal windows and a short name in mind for each worktree.
1. Confirm the repository state
At the project root, inspect version-control history to confirm at least one commit. Inspect the full repository state, record existing changes and decide whether to commit or stash them. Record each worktree task’s starting commit for the complete task review.
2. Start the feature task in terminal one
From the project root, run the documented command form: claude --worktree feature-auth, where feature-auth is a name you choose. Replace it with one that fits your task. In the session, enter the full task description: the goal, the directories involved, the acceptance criteria, and the instruction to work on this one thing only.
3. Start the bug task in terminal two
Open a second terminal, go to the same project root, and run the same command with a different name, for example claude --worktree bugfix-login. The documentation says running the same command with a different name in a second terminal starts an isolated parallel session. The input should include the reproduction command, the error and the expected result, and should not refer to the first task.
4. Let each session work and keep them apart
Each session reads and edits the files in its own separate checkout. Put the acceptance method in each session's instructions, such as which test command to run. When you switch between sessions, first confirm which terminal and name you are looking at so you do not send one task's instruction to the other session. To monitor several parallel sessions on one screen, the documentation points to background agents, which this guide does not cover.
5. Check each diff and test run
In each worktree, inspect the entire task change relative to its starting commit through version control, including tracked modifications, untracked files and commits made during the task. A clean uncommitted diff does not replace review. Check every changed file against task scope and inspect the specified test’s real output. Revert only this task’s changes within its checkout; inspect external-service effects separately.
6. Decide how to finish or stop
Commit and merge the tasks you are happy with using your team's process. For any you do not want, stop the session and discard its branch changes. The documentation says worktree cleanup, .worktreeinclude and non-git version control support are described on its Worktrees page, so read that before cleaning up and confirm no valuable uncommitted work remains. To continue later, run claude --continue in the same directory to resume the most recent session.
Original example
The following is a fictional scenario that only illustrates phrasing; it is not real output. Project: shop-demo, which already has commits. Task A: add a "remember me" option to the login module. Task B: fix a bug where checkout is still allowed when the cart quantity is 0. Terminal 1: claude --worktree feature-remember-me Input: Add a "remember me" option to the login module, changing only files under src/auth; when done run npm test and tell me the result. Terminal 2: claude --worktree bugfix-empty-cart Input: When I run npm test, an empty cart can still check out, every time. Explain the cause first, then fix it, then rerun npm test. Expected checks: each worktree's complete task change relative to its starting commit includes only its own task files, including untracked files and committed task changes; rerunning the same command in bugfix-empty-cart no longer shows the original symptom; neither task's changes appear in the other's diff.
Completion checks
- History confirms at least one commit, the two claude --worktree names differ, and each task’s starting commit is recorded.
- In each worktree, inspect the entire task change relative to its starting commit through version control, including tracked modifications, untracked files and commits made during the task. A clean uncommitted diff does not replace review. Check every changed file against task scope and inspect the specified test’s real output. Revert only this task’s changes within its checkout; inspect external-service effects separately.
- Each session ran the test command you specified, and you compared the real output rather than relying on a description.
- You know whether each task will be merged, kept or discarded, and no valuable uncommitted work is left behind.
Limits
- A worktree isolates only the file checkout. It does not guarantee the two tasks are logically independent, and editing the same function in both can still conflict at merge time.
- The documentation requires at least one commit first; a brand-new repository must be committed before this works.
- This guide does not cover worktree cleanup details, .worktreeinclude configuration, non-git version control or background agents; see the official Worktrees documentation.
- Worktrees do not undo remote actions such as databases, APIs or deployments. If both tasks touch the same external service, you must control the effects yourself.
Verification scope
This guide is based on reading the official documentation. Login, installation, product command or API execution, file upload and generated output were not tested; the project, task names and inputs in the example are original illustrations with no real run results.
Article sources
Original editorial content; official descriptions and checking suggestions are distinguished.