isolation: "worktree" branches from origin/HEAD instead of current HEAD
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
- Be on a feature branch:
git checkout feat/my-feature origin/HEADpoints tomain(the remote default)- Spawn an agent with
isolation: "worktree" - The worktree branches from
origin/main, notfeat/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
HEADby default (matching standardgit worktree addbehavior) - Or add a config option to choose the base:
origin/HEADvsHEAD
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗