Claude Code silently deletes clean worktrees on exit; git worktree lock offers a fix
Claude Code version 2.1.263 automatically removes a worktree and its associated branch when a session ends with no uncommitted changes and no new commits, without prompting the user. This is documented intended behavior rather than a bug, and no built-in setting exists to disable the auto-cleanup. The tool checks git status and commit count at exit, and only shows a Keep/Remove dialog if changes or new commits are present, or if the session was explicitly named. A workaround involves running git worktree lock with a custom reason string, since Claude Code's cleanup routine skips worktrees locked by a reason it did not write itself. A SessionStart hook can automate this re-locking to protect worktrees from silent deletion every session.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in