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
- Start a Claude Code session
- Create a git worktree (e.g., via
EnterWorktreeor manually) - Work in the worktree — shell CWD is inside the worktree directory
- Complete work: commit, push, merge back to main
- Delete the worktree (either manually via
git worktree removeor auto-cleanup) - 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:
- Auto-detect: Before executing shell commands, check if CWD exists and reset to project root if not
- Preventive: When removing a worktree,
cdback to the main repo root before the removal command - 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/
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗