[BUG] `isolation: "worktree"` creates worktrees from wrong commit when base branch is not `main`

Resolved 💬 3 comments Opened Apr 4, 2026 by justinrknowles Closed Apr 8, 2026

Preflight Checklist

  • [x] I have searched existing issues and this hasn't been reported yet
  • [x] This is a single bug report (please file separate reports for different bugs)
  • [x] I am using the latest version of Claude Code

What's Wrong?

When using isolation: "worktree" on an Agent tool call, the worktree is created from the wrong commit — typically origin/main or another remote ref — instead of the current session's HEAD. This makes worktree isolation unreliable for any workflow where the active branch is not main.

What Should Happen?

The worktree should be created from aaa111 (HEAD of my-clean-branch, the branch checked out in the session). The worktree should contain only the files from my-clean-branch.

Error Messages/Logs

The worktree is created from `bbb222` (tip of `origin/main`). It contains files from the `main` branch history, not from the session's active branch.


/path/to/repo                              aaa111 [my-clean-branch]  ← correct (main worktree)
/path/to/.claude/worktrees/agent-xxx       bbb222 [worktree-agent-xxx]  ← WRONG

Steps to Reproduce

  1. Create an orphan branch (or any branch that diverges from main):

``bash
git checkout --orphan my-clean-branch
# ... add files, commit ...
# my-clean-branch is now at commit aaa111
# origin/main is at commit bbb222 (different history)
``

  1. From a Claude Code session on my-clean-branch, invoke an agent with worktree isolation:

``
Agent(
isolation: "worktree",
prompt: "...",
subagent_type: "general-purpose"
)
``

  1. Check the worktree that was created:

``bash
git worktree list
``

Claude Model

Opus

Is this a regression?

I don't know

Last Working Version

_No response_

Claude Code Version

2.1.69 (Claude Code)

Platform

AWS Bedrock

Operating System

macOS

Terminal/Shell

Terminal.app (macOS)

Additional Information

isolation: "worktree" creates worktrees from wrong commit when base branch is not main

Description

When using isolation: "worktree" on an Agent tool call, the worktree is created from the wrong commit — typically origin/main or another remote ref — instead of the current session's HEAD. This makes worktree isolation unreliable for any workflow where the active branch is not main.

Steps to Reproduce

  1. Create an orphan branch (or any branch that diverges from main):

``bash
git checkout --orphan my-clean-branch
# ... add files, commit ...
# my-clean-branch is now at commit aaa111
# origin/main is at commit bbb222 (different history)
``

  1. From a Claude Code session on my-clean-branch, invoke an agent with worktree isolation:

``
Agent(
isolation: "worktree",
prompt: "...",
subagent_type: "general-purpose"
)
``

  1. Check the worktree that was created:

``bash
git worktree list
``

Expected Behavior

The worktree should be created from aaa111 (HEAD of my-clean-branch, the branch checked out in the session). The worktree should contain only the files from my-clean-branch.

Actual Behavior

The worktree is created from bbb222 (tip of origin/main). It contains files from the main branch history, not from the session's active branch.

/path/to/repo                              aaa111 [my-clean-branch]  ← correct (main worktree)
/path/to/.claude/worktrees/agent-xxx       bbb222 [worktree-agent-xxx]  ← WRONG

Root Cause

The underlying git worktree add -b <branch> <path> command is missing an explicit start-point argument. Without one, git resolves the start point using its own heuristics — which in many cases picks a remote tracking ref like origin/main over the current working tree's HEAD.

Suggested Fix

Pass HEAD (or the current branch ref) as the explicit start point:

# Instead of:
git worktree add -b worktree-agent-xxx .claude/worktrees/agent-xxx

# Use:
git worktree add -b worktree-agent-xxx .claude/worktrees/agent-xxx HEAD

Additional Context

  • Deleting local branches that match the remote refs does not fix the issue — the remote tracking refs (origin/main, etc.) keep the objects alive and the worktree mechanism still resolves to them.
  • Manual worktree creation with an explicit start point works correctly:

``bash
git worktree add --no-track -b test-branch .claude/worktrees/test-wt my-clean-branch
# Correctly creates worktree at aaa111
``

  • This is particularly impactful for workflows that use non-default base branches (release branches, orphan branches, feature branch workflows).

Environment

  • Claude Code CLI
  • macOS (Darwin 25.3.0)
  • git version (standard macOS)

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗