claude-code - 💡(How to fix) Fix Worktree sessions report the wrong branch in the system prompt [1 comments, 2 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#53278Fetched 2026-04-26 05:19:48
View on GitHub
Comments
1
Participants
2
Timeline
4
Reactions
0
Timeline (top)
labeled ×3commented ×1

When a Claude Code session is started inside a git worktree, the system prompt gitStatus block reports a Current branch that doesn't match the worktree actual checked-out branch.

What it reports instead: the branch the main clone happened to be on at the moment the worktree was created. That value gets frozen into the worktree session metadata and never updated.

Root Cause

When a Claude Code session is started inside a git worktree, the system prompt gitStatus block reports a Current branch that doesn't match the worktree actual checked-out branch.

What it reports instead: the branch the main clone happened to be on at the moment the worktree was created. That value gets frozen into the worktree session metadata and never updated.

RAW_BUFFERClick to expand / collapse

Summary

When a Claude Code session is started inside a git worktree, the system prompt gitStatus block reports a Current branch that doesn't match the worktree actual checked-out branch.

What it reports instead: the branch the main clone happened to be on at the moment the worktree was created. That value gets frozen into the worktree session metadata and never updated.

Repro

Any Claude Code session whose cwd is inside a git worktree directory exhibits this. The gitStatus block reports the main clone branch (the one checked out in the original repo directory) as Current branch, instead of the worktree actual HEAD.

Evidence from JSONL

The session state file at the per-project projects directory records two distinct branch fields in the worktree-state record:

  • worktreeBranch: actual checkout in the worktree (correct)
  • originalBranch: branch checked out in the main clone at worktree-creation time (frozen snapshot)

The per-message gitBranch field, and the system-prompt gitStatus Current branch line, both source from originalBranch instead of the live worktree HEAD.

Expected

The harness should ask git directly what branch the worktree is on, every time it builds the system prompt. Right now it is reading the answer from a value saved when the worktree was first created -- that value never updates.

Why it matters

Agents trust the system-prompt header as ground truth. When it is wrong:

  • An agent may believe an active scope is in flight when it is not, and skip the kickoff step.
  • An agent may push to or operate on a branch that has already been merged.
  • The mismatch is silent -- no warning.

Diagnosis trail with full JSONL evidence: karyandrew/second-brain#301

extent analysis

TL;DR

The system prompt's gitStatus block should be updated to query the current branch directly from the git worktree instead of relying on the frozen value from the worktree's creation time.

Guidance

  • The issue arises because the originalBranch field in the worktree-state record is used as the source for the gitBranch field and the system-prompt gitStatus Current branch line, instead of the live worktreeBranch or directly querying the git worktree.
  • To verify the issue, compare the worktreeBranch and originalBranch fields in the session state file to the actual branch checked out in the worktree.
  • The harness should be modified to query the git worktree directly for its current branch when building the system prompt, rather than relying on the saved originalBranch value.
  • Reviewing the diagnosis trail and JSONL evidence in the linked issue (karyandrew/second-brain#301) may provide further insight into the problem.

Notes

The exact implementation details of querying the git worktree for its current branch are not specified in the issue, so the modification to the harness will depend on the specific git commands or APIs available.

Recommendation

Apply a workaround to update the system prompt's gitStatus block to query the current branch directly from the git worktree, as this will ensure that the harness has the correct information about the worktree's current branch.

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…

Still need to ship something?

×6

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

Back to top recommendations

TRENDING