Shell session breaks when worktree CWD is deleted during merge/cleanup

Resolved 💬 3 comments Opened Feb 28, 2026 by ujjalcal Closed Mar 4, 2026

Summary

When using git worktrees via Claude Code, the shell session becomes permanently stuck after the worktree directory is cleaned up — because the shell's CWD no longer exists. All subsequent commands fail with "no such file or directory."

Steps to Reproduce

  1. Start a Claude Code session
  2. Create a git worktree (e.g., via EnterWorktree or manually)
  3. Work in the worktree — shell CWD is inside the worktree directory
  4. Complete work: commit, push, merge back to main
  5. Delete the worktree (either manually via git worktree remove or auto-cleanup)
  6. Try to run any shell command → fails because CWD no longer exists

Expected Behavior

Claude Code should detect when the CWD no longer exists and automatically reset to the project root (or the main repo checkout). The session should remain functional.

Actual Behavior

The shell session is permanently broken. No commands can execute. The user must restart the session to recover.

Suggested Fix

Either:

  1. Auto-detect: Before executing shell commands, check if CWD exists and reset to project root if not
  2. Preventive: When removing a worktree, cd back to the main repo root before the removal command
  3. Recovery: If a command fails with ENOENT/ENOTDIR on CWD, automatically reset CWD and retry

Environment

  • Claude Code CLI (VSCode extension)
  • macOS (Darwin 24.6.0)
  • Git worktrees via .claude/worktrees/

View original on GitHub ↗

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