Claude Code worktrees. From start to cleanup.
A practical Git worktree walkthrough for Claude Code: start from a known base, isolate local services, inspect the diff and remove a finished checkout safely.
A Git worktree is another checkout of the same repository, with its own working files and branch. It is useful when an agent needs to change code while you keep working elsewhere. It does not create a separate computer or a separate database.
Start from a known base
Before creating the task checkout, check which repository and branch you are using. Uncommitted work in your current checkout will not magically appear in the new one. Decide whether the task should start from your current commit or the latest remote main.
git status --short
git branch --show-current
git fetch origin
git worktree add -b fix/login-loop ../app-login-loop origin/main
git -C ../app-login-loop status --shortThis example creates a new branch from origin/main; replace that base if your project uses another target. Open the new folder in a terminal, then run claude. Do not also run claude --worktree inside it unless you intend to create another checkout.
Or let Claude Code create the worktree
claude --worktree login-loopThis is the shorter route when Claude should manage creation. It requires a Git repository with an existing commit. Check the folder and branch Claude reports before giving it editing work. Use the manual route above when selecting an explicit starting commit matters.
Prepare the parts Git does not copy
A clean checkout needs the project’s normal dependency installation. Follow its lockfile and setup instructions rather than copying another agent’s dependency folder. Local environment files may be ignored by Git; supply only the configuration the task needs through your established setup.
- Choose an unused development-server port and record it in the task brief.
- Give integration tests an isolated test database or namespace if they write data.
- Check whether build caches and generated output are shared.
- Run the relevant baseline test before blaming a later failure on the agent’s change.
Review against the actual target
Ask for a focused patch and the exact checks. From the task checkout, inspect the branch diff and uncommitted changes separately:
git diff --stat origin/main...HEAD
git diff origin/main...HEAD
git diff
git status --shortThe three-dot diff shows committed branch changes since the common ancestor. Plain git diff catches unstaged edits; check staged edits with git diff --cached if any exist. Run your project’s required verification on the final combined change, especially after resolving conflicts.
Remove the checkout only after preserving the work
First confirm the branch is merged or otherwise safely retained, and stop that checkout’s development processes. From the original checkout:
git worktree list
git -C ../app-login-loop status --short
git worktree remove ../app-login-loopIf removal refuses because files remain, inspect them. Avoid forcing removal just to tidy the list. Keeping the branch is fine; delete it only after confirming you no longer need it. Next: verify an agent’s “all tests pass” claim.
Command references checked October 15, 2026: Git worktree reference and Claude Code worktrees. Check your installed CLI’s help if a flag is unavailable.
Common questions
What does claude --worktree do?
It starts Claude Code in a separate Git worktree. Run it from a Git repository that already has a commit, and use a different worktree name for each independent task.
Do worktrees isolate databases and ports?
No. They separate tracked files and branches, but local services, ports, caches and credentials need their own configuration.