{"record":{"id":"33d3ab64bd14aaeb","repo":"jesseduffield/lazygit","slug":"you-are-already-in-the-selected-worktree","errorCode":null,"errorMessage":"You are already in the selected worktree","messagePattern":"You are already in the selected worktree","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"info","filePath":"pkg/gui/controllers/helpers/worktree_helper.go","lineNumber":114,"sourceCode":"\t\tprompt := utils.ResolvePlaceholderString(self.c.Tr.WorktreeLocationPromptTrackingBranch,\n\t\t\tmap[string]string{\"name\": branchName, \"ref\": value})\n\t\treturn self.promptForWorktreeLocation(branchName, prompt, func(path string) error {\n\t\t\treturn self.createWorktree(git_commands.NewWorktreeOpts{Path: path, Base: value, Branch: branchName}, context.WORKTREES_CONTEXT_KEY)\n\t\t})\n\t}\n\n\tname := SanitizedBranchName(value)\n\tbase := self.refsHelper.GetCheckedOutRef().RefName()\n\tprompt := utils.ResolvePlaceholderString(self.c.Tr.WorktreeLocationPromptNewBranch,\n\t\tmap[string]string{\"name\": name, \"base\": base})\n\treturn self.promptForWorktreeLocation(name, prompt, func(path string) error {\n\t\treturn self.createWorktree(git_commands.NewWorktreeOpts{Path: path, Base: base, Branch: name}, context.WORKTREES_CONTEXT_KEY)\n\t})\n}\n\nfunc (self *WorktreeHelper) Switch(worktree *models.Worktree, contextKey types.ContextKey) error {\n\tif worktree.IsCurrent {\n\t\treturn errors.New(self.c.Tr.AlreadyInWorktree)\n\t}\n\n\tself.c.LogAction(self.c.Tr.SwitchToWorktree)\n\n\treturn self.reposHelper.DispatchSwitchTo(worktree.Path, self.c.Tr.ErrWorktreeMovedOrRemoved, contextKey)\n}\n\n// Remove deletes the worktree without confirming first; callers are expected to\n// have confirmed (or shown a menu) already. If git refuses because the worktree\n// is dirty or contains submodules, we ask for confirmation and retry with\n// --force. When then is non-nil it runs in place of the default refresh after a\n// successful removal, letting callers chain further work such as deleting the\n// worktree's branch.\nfunc (self *WorktreeHelper) Remove(worktree *models.Worktree, then func(gocui.Task) error) error {\n\treturn self.remove(worktree, false, then)\n}\n\nfunc (self *WorktreeHelper) remove(worktree *models.Worktree, force bool, then func(gocui.Task) error) error {","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/jesseduffield/lazygit/blob/c477a2959b229fbf3284be0d4d2904ab61ec3c94/pkg/gui/controllers/helpers/worktree_helper.go#L96-L132","documentation":"Returned by WorktreeHelper.Switch when the worktree passed in has IsCurrent == true, i.e. the user selected the worktree lazygit is already running in. Switching to the worktree you are in is a no-op at the git level, so it is rejected with a clear message instead.","triggerScenarios":"Pressing enter/switch on the worktrees panel entry whose models.Worktree.IsCurrent is set; or calling WorktreeHelper.Switch(worktree, contextKey) programmatically with the current worktree object.","commonSituations":"Worktrees list refreshed while the user was navigating so the selection lands on the current entry; keybinding macros or scripts that iterate all worktrees and switch to each.","solutions":["Select a different worktree in the worktrees panel before invoking switch.","In code, check worktree.IsCurrent before calling Switch and skip or log instead.","Refresh the worktrees view if you suspect stale IsCurrent flags (e.g. after switching repos or external worktree removal)."],"exampleFix":"// before\nerr := helper.Switch(worktree, context.WORKTREES_CONTEXT_KEY)\n\n// after\nif worktree.IsCurrent {\n    return nil // already here; nothing to do\n}\nerr := helper.Switch(worktree, context.WORKTREES_CONTEXT_KEY)","handlingStrategy":"validation","validationCode":"if worktree.IsCurrent {\n    return nil // already in this worktree; skip Switch\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check IsCurrent before calling WorktreeHelper.Switch.","Disable or hide the switch action for the current worktree's row in menu construction.","Refresh the worktrees model before bulk switch operations so IsCurrent is accurate."],"tags":["git","worktree","no-op-guard"],"backgroundTag":null,"analyzedSha":"c477a2959b229fbf3284be0d4d2904ab61ec3c94","analyzedAt":"2026-08-15T08:34:32.451Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}