claude-code - 💡(How to fix) Fix Session-scoped working directory / git branch state [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#49429Fetched 2026-04-17 08:41:26
View on GitHub
Comments
0
Participants
1
Timeline
3
Reactions
0
Participants
Timeline (top)
labeled ×3

Error Message

  1. Branch lock on the session. When a session creates or checks out a branch for a ticket, record that as the session's intended branch. Warn (or block) when commit/push targets a different branch. Could be a harness hook rather than a git feature.

Root Cause

Each time I ran git commit && git push expecting to be on the ticket branch. The first time I caught it before pushing. The second time it was only noticed because the user flagged "you're on the wrong branch again."

Fix Action

Fix / Workaround

I was working on a ticket (branch: fix/tes-2325-...), created off develop, with multiple commits in progress. Between tool calls the working directory and current branch got switched — twice — to unrelated branches (patch/TES-2316, fix/tes-2336-...). I didn't initiate those checkouts; they appear to have come from parallel agents / side-tasks running in the same session.

Why a memory/workaround isn't enough

RAW_BUFFERClick to expand / collapse

Problem

Subagents, side-tasks, scheduled tasks, and worktree tooling all share the same process-level working directory and git state as the main session. Any of them can run git checkout (or cd) and silently change the state that the main session was relying on.

What happened in my session

I was working on a ticket (branch: fix/tes-2325-...), created off develop, with multiple commits in progress. Between tool calls the working directory and current branch got switched — twice — to unrelated branches (patch/TES-2316, fix/tes-2336-...). I didn't initiate those checkouts; they appear to have come from parallel agents / side-tasks running in the same session.

Each time I ran git commit && git push expecting to be on the ticket branch. The first time I caught it before pushing. The second time it was only noticed because the user flagged "you're on the wrong branch again."

If the mismatch had slipped through, I would have pushed commits to someone else's in-flight PR branch. That's an easy footgun to land a bad diff on an unrelated review.

Why a memory/workaround isn't enough

I saved a feedback memory to re-check git branch --show-current immediately before every commit/push. That reduces the chance but doesn't eliminate it — anything that happens between the check and the push can still flip the branch. And it puts the burden on the model to remember a defensive action rather than the harness providing isolation.

Suggested fixes (any one would help)

  1. Session-scoped cwd / branch state. Each session gets an isolated view of the repo's HEAD and working directory. Worktrees already provide this; make it the default for fresh sessions that are "working on" a branch or ticket.
  2. Branch lock on the session. When a session creates or checks out a branch for a ticket, record that as the session's intended branch. Warn (or block) when commit/push targets a different branch. Could be a harness hook rather than a git feature.
  3. Scoped clones for subagents/side-tasks. Subagents that need to run git operations should do so in a scoped clone/worktree, not on the user's live checkout.

Option 1 is the cleanest mental model and what most users probably already assume is true.

Repro

Open a session, create a branch, start doing work on it. Trigger a spawn_task / side-task that touches the repo (anything that runs git checkout). Return to the original session and inspect git branch --show-current — it's no longer the branch you started on, with no indication to the model that it changed.

extent analysis

TL;DR

Implement session-scoped cwd/branch state to prevent unintended branch changes caused by parallel agents or side-tasks.

Guidance

  • Consider using worktrees to provide isolated views of the repo's HEAD and working directory for each session.
  • Evaluate the feasibility of implementing a branch lock on the session to warn or block commit/push operations targeting a different branch.
  • Investigate using scoped clones for subagents/side-tasks to prevent them from modifying the user's live checkout.
  • Verify the current branch before committing or pushing changes to ensure it matches the intended branch.

Example

No code snippet is provided as it is not explicitly supported by the issue.

Notes

The suggested fixes aim to address the issue of unintended branch changes caused by parallel agents or side-tasks. However, the implementation details and potential trade-offs are not discussed in the issue.

Recommendation

Apply workaround: Implement session-scoped cwd/branch state to provide isolation for each session, as it is considered the cleanest mental model and aligns with user expectations.

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

claude-code - 💡(How to fix) Fix Session-scoped working directory / git branch state [1 participants]