Skip to content

Auto-switch to main worktree when linked worktree is deleted externally#5367

Open
CJHwong wants to merge 2 commits intojesseduffield:masterfrom
CJHwong:fix/recover-deleted-worktree
Open

Auto-switch to main worktree when linked worktree is deleted externally#5367
CJHwong wants to merge 2 commits intojesseduffield:masterfrom
CJHwong:fix/recover-deleted-worktree

Conversation

@CJHwong
Copy link

@CJHwong CJHwong commented Mar 13, 2026

PR Description

Relates to #5266. Alternative approach to #5355.

When lazygit is running inside a linked worktree and that directory gets deleted externally (e.g. a Claude Code session cleaning up its worktree, or rm -rf from another terminal), every git command fails and lazygit panics in obtainBranches.

Instead of exiting with an error (#5355's approach), this detects the missing CWD at the top of Refresh and switches back to the main worktree automatically. The user keeps their lazygit session. A toast notification informs the user that the switch happened. If the main worktree itself is gone, it panics since there's nowhere to recover to.

One platform subtlety: on macOS, os.Stat(".") succeeds on a deleted directory because the kernel holds the inode alive. The detection has to stat the absolute path from os.Getwd() instead. On Linux, os.Getwd() itself fails with ENOENT. Both cases are handled.

Changes:

  • RepoPaths.InLinkedWorktree(): returns whether we're in a linked worktree
  • RefreshHelper.Refresh(): checks if CWD is gone before running git commands, uses sync.Once to avoid duplicate recovery from concurrent refresh goroutines
  • Shows a toast notification ("Worktree deleted externally. Switched to main worktree.") so the user knows what happened (addresses feedback from Exit cleanly when worktree/repository is removed while lazygit is running #5355)
  • Integration test that creates a worktree, starts lazygit in it, deletes it, and verifies lazygit lands on the main worktree with the expected toast

Please check if the PR fulfills these requirements

  • Cheatsheets are up-to-date (run go generate ./...)
  • Code has been formatted (see here)
  • Tests have been added/updated (see here for the integration test guide)
  • Text is internationalised (see here)
  • If a new UserConfig entry was added, make sure it can be hot-reloaded (see here) - no new config entries
  • Docs have been updated if necessary
  • You've read through your own file changes for silly mistakes etc

When a linked worktree's directory is deleted while lazygit is running
(e.g. from another terminal), every git command fails and lazygit either
panics or shows empty panels. Detect this at the start of Refresh by
checking whether the CWD still exists on disk, and if the current
worktree is a linked worktree, automatically switch to the main worktree
instead of crashing. If the main worktree itself is gone, panic as before
since there is nothing to recover to.

Handles both Linux (os.Getwd fails) and macOS (os.Getwd succeeds but the
path no longer exists in the directory tree) CWD deletion behavior.
Users had no visual feedback when lazygit auto-switched to the main
worktree after the linked worktree was deleted externally. Use ErrorToast
(4s display) so the message is readable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant