jesseduffield/lazygit · info

You are already in the selected worktree

Error message

You are already in the selected worktree

What it means

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.

Source

Thrown at pkg/gui/controllers/helpers/worktree_helper.go:114

		prompt := utils.ResolvePlaceholderString(self.c.Tr.WorktreeLocationPromptTrackingBranch,
			map[string]string{"name": branchName, "ref": value})
		return self.promptForWorktreeLocation(branchName, prompt, func(path string) error {
			return self.createWorktree(git_commands.NewWorktreeOpts{Path: path, Base: value, Branch: branchName}, context.WORKTREES_CONTEXT_KEY)
		})
	}

	name := SanitizedBranchName(value)
	base := self.refsHelper.GetCheckedOutRef().RefName()
	prompt := utils.ResolvePlaceholderString(self.c.Tr.WorktreeLocationPromptNewBranch,
		map[string]string{"name": name, "base": base})
	return self.promptForWorktreeLocation(name, prompt, func(path string) error {
		return self.createWorktree(git_commands.NewWorktreeOpts{Path: path, Base: base, Branch: name}, context.WORKTREES_CONTEXT_KEY)
	})
}

func (self *WorktreeHelper) Switch(worktree *models.Worktree, contextKey types.ContextKey) error {
	if worktree.IsCurrent {
		return errors.New(self.c.Tr.AlreadyInWorktree)
	}

	self.c.LogAction(self.c.Tr.SwitchToWorktree)

	return self.reposHelper.DispatchSwitchTo(worktree.Path, self.c.Tr.ErrWorktreeMovedOrRemoved, contextKey)
}

// Remove deletes the worktree without confirming first; callers are expected to
// have confirmed (or shown a menu) already. If git refuses because the worktree
// is dirty or contains submodules, we ask for confirmation and retry with
// --force. When then is non-nil it runs in place of the default refresh after a
// successful removal, letting callers chain further work such as deleting the
// worktree's branch.
func (self *WorktreeHelper) Remove(worktree *models.Worktree, then func(gocui.Task) error) error {
	return self.remove(worktree, false, then)
}

func (self *WorktreeHelper) remove(worktree *models.Worktree, force bool, then func(gocui.Task) error) error {

View on GitHub (pinned to c477a2959b)

Solutions

  1. Select a different worktree in the worktrees panel before invoking switch.
  2. In code, check worktree.IsCurrent before calling Switch and skip or log instead.
  3. Refresh the worktrees view if you suspect stale IsCurrent flags (e.g. after switching repos or external worktree removal).

Example fix

// before
err := helper.Switch(worktree, context.WORKTREES_CONTEXT_KEY)

// after
if worktree.IsCurrent {
    return nil // already here; nothing to do
}
err := helper.Switch(worktree, context.WORKTREES_CONTEXT_KEY)
Defensive patterns

Strategy: validation

Validate before calling

if worktree.IsCurrent {
    return nil // already in this worktree; skip Switch
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of jesseduffield/lazygit@c477a2959b (2026-08-15). Data as JSON: /api/errors/33d3ab64bd14aaeb. Report an issue: GitHub.