claude-code - 💡(How to fix) Fix isolation: "worktree" branches from origin/HEAD instead of current HEAD [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#48095Fetched 2026-04-15 06:33:21
View on GitHub
Comments
0
Participants
1
Timeline
2
Reactions
0
Author
Participants
Timeline (top)
labeled ×2

Fix Action

Workaround

We added a PreToolUse hook on Agent in .claude/settings.json that runs git remote set-head origin $(git branch --show-current) before every agent spawn. This updates origin/HEAD to match the current branch so the worktree branches from the correct commit.

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Agent",
        "hooks": [
          {
            "type": "command",
            "command": "current_branch=$(git branch --show-current 2>/dev/null); if [ -n \"$current_branch\" ]; then git remote set-head origin \"$current_branch\" 2>/dev/null; fi; exit 0",
            "timeout": 5
          }
        ]
      }
    ]
  }
}

Code Example

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Agent",
        "hooks": [
          {
            "type": "command",
            "command": "current_branch=$(git branch --show-current 2>/dev/null); if [ -n \"$current_branch\" ]; then git remote set-head origin \"$current_branch\" 2>/dev/null; fi; exit 0",
            "timeout": 5
          }
        ]
      }
    ]
  }
}
RAW_BUFFERClick to expand / collapse

Problem

When spawning an agent with isolation: "worktree", the worktree is created from origin/HEAD (the remote default branch), not the current branch HEAD. This means if you're working on a feature branch, the worktree gets code from an entirely different (often months-old) commit.

Steps to reproduce

  1. Be on a feature branch: git checkout feat/my-feature
  2. origin/HEAD points to main (the remote default)
  3. Spawn an agent with isolation: "worktree"
  4. The worktree branches from origin/main, not feat/my-feature

Expected behavior

The worktree should branch from the current HEAD (the commit you're actually working on), matching what git worktree add <path> -b <branch> HEAD does.

Workaround

We added a PreToolUse hook on Agent in .claude/settings.json that runs git remote set-head origin $(git branch --show-current) before every agent spawn. This updates origin/HEAD to match the current branch so the worktree branches from the correct commit.

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Agent",
        "hooks": [
          {
            "type": "command",
            "command": "current_branch=$(git branch --show-current 2>/dev/null); if [ -n \"$current_branch\" ]; then git remote set-head origin \"$current_branch\" 2>/dev/null; fi; exit 0",
            "timeout": 5
          }
        ]
      }
    ]
  }
}

Impact

Without the workaround, every agent with worktree isolation gets stale code. When their output is copied back to the main repo, it silently overwrites current code with old versions. In our case, this caused 15+ CLI commands to be deleted and hours of debugging.

Suggestion

Either:

  • Branch worktrees from HEAD by default (matching standard git worktree add behavior)
  • Or add a config option to choose the base: origin/HEAD vs HEAD

extent analysis

TL;DR

To fix the issue, update the origin/HEAD to match the current branch before spawning an agent with isolation: "worktree".

Guidance

  • Use the provided PreToolUse hook in .claude/settings.json to update origin/HEAD before every agent spawn.
  • Verify that the worktree is branching from the correct commit by checking the commit history after spawning the agent.
  • Consider adding a config option to choose the base for worktree isolation, allowing users to opt for either origin/HEAD or HEAD.
  • Test the workaround in different scenarios, such as when working on a feature branch or when origin/HEAD points to a different branch.

Example

The provided JSON configuration snippet demonstrates how to implement the PreToolUse hook:

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Agent",
        "hooks": [
          {
            "type": "command",
            "command": "current_branch=$(git branch --show-current 2>/dev/null); if [ -n \"$current_branch\" ]; then git remote set-head origin \"$current_branch\" 2>/dev/null; fi; exit 0",
            "timeout": 5
          }
        ]
      }
    ]
  }
}

Notes

This workaround relies on the git remote set-head command, which may not be suitable for all use cases. Additionally, the PreToolUse hook may introduce additional overhead or complexity.

Recommendation

Apply the workaround using the PreToolUse hook, as it provides a functional solution to the issue. This approach allows users to continue working with isolation: "worktree" while ensuring that the worktree branches from the correct commit.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

The worktree should branch from the current HEAD (the commit you're actually working on), matching what git worktree add <path> -b <branch> HEAD does.

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING