isolation: "worktree" branches from origin/HEAD instead of current HEAD

Resolved 💬 2 comments Opened Apr 14, 2026 by ZaxShen Closed Jun 2, 2026

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

View original on GitHub ↗

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