Auto-switch to main worktree when linked worktree is deleted externally#5367
Open
CJHwong wants to merge 2 commits intojesseduffield:masterfrom
Open
Auto-switch to main worktree when linked worktree is deleted externally#5367CJHwong wants to merge 2 commits intojesseduffield:masterfrom
CJHwong wants to merge 2 commits intojesseduffield:masterfrom
Conversation
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.
This was referenced Mar 13, 2026
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 -rffrom another terminal), every git command fails and lazygit panics inobtainBranches.Instead of exiting with an error (#5355's approach), this detects the missing CWD at the top of
Refreshand 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 fromos.Getwd()instead. On Linux,os.Getwd()itself fails withENOENT. Both cases are handled.Changes:
RepoPaths.InLinkedWorktree(): returns whether we're in a linked worktreeRefreshHelper.Refresh(): checks if CWD is gone before running git commands, usessync.Onceto avoid duplicate recovery from concurrent refresh goroutinesPlease check if the PR fulfills these requirements
go generate ./...)