Multiple Claude Code sessions in same repo can silently swap each other's branch

Open 💬 4 comments Opened May 18, 2026 by erickcardeal

Summary

When running two Claude Code sessions in the same working directory on different branches, one session can git checkout a different branch and silently affect the other session's working tree. The other session has no way to know its mental model of the current branch is now stale, leading to confusion, wasted time, and potential dataloss if Claude attempts to "fix" the broken state with destructive operations.

Environment

  • Claude Code CLI
  • macOS (Darwin 25.3.0)
  • Single repo, single working directory, two terminal sessions running Claude Code in parallel

Scenario (real, observed 2026-05-18)

  1. Session A: working on feature/cancelamento-plano-2026-05-18 (E2E tests for a plan cancellation flow)
  2. Session B (parallel terminal, same repo dir): working on fix/timestamps-lista-compras (dashboard timestamp changes)
  3. Session A checks out feature/..., edits files, commits (648eaf23)
  4. Session B checks out back to fix/... — this affects the global working tree because both sessions share the same .git/ and worktree, even though Session A is still "active" on its branch in its own terminal
  5. Session A continues believing it's still on feature/... (its mental model wasn't invalidated), edits docs, tries to run E2E tests
  6. Everything breaks because the files in the working tree now reflect the other branch
  7. Session A has to git stash + re-checkout + git stash pop multiple times during the session just to keep working

Reflog evidence

40ddd1cc HEAD@{0}: checkout: moving from feature/cancelamento-plano-2026-05-18 to fix/timestamps-lista-compras
648eaf23 HEAD@{1}: checkout: moving from fix/timestamps-lista-compras to feature/cancelamento-plano-2026-05-18
648eaf23 HEAD@{3}: commit: refactor(cancelamento-plano)...
12d2180b HEAD@{6}: checkout: moving from fix/timestamps-lista-compras to feature/cancelamento-plano-2026-05-18

Multiple checkouts back and forth in a single session — each one was the other Claude session pulling the working tree out from under the first.

Impact

  • Lost mental context — "why did my changes disappear?"
  • Time wasted investigating phantom problems (files that were there 30 seconds ago are now gone or different)
  • Risk of dataloss — if Claude doesn't detect the swap and tries git reset --hard or git checkout . to "fix" the broken state, in-progress edits from the other session can be destroyed
  • Confusion about which session is doing what

Suggested mitigations

  1. Warn when another Claude Code session is active in the same repo — lockfile in .claude/, PID detection, or a ps scan on startup
  2. Detect external branch/HEAD changes between tool calls — compare git rev-parse HEAD and current branch against the last-known value before issuing actions; if they differ, surface a clear message to the user and pause before destructive operations
  3. Document recommended workflow for multi-session work in the same repogit worktree is the natural solution but isn't obvious to most users

Current workaround: git worktree

For users hitting this, use git worktree add to give each branch its own directory:

git worktree add ../myrepo-feature feature/cancelamento-plano-2026-05-18
git worktree add ../myrepo-fix fix/timestamps-lista-compras

Then open each Claude Code session in its respective worktree directory. The working trees are isolated even though they share the same .git/ and history, so checkouts in one no longer clobber the other.

This works well but it would be much better if Claude Code either prevented the footgun or surfaced it loudly when it happens.

View original on GitHub ↗

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