jesseduffield/lazygit · warning
Branch {{.branchName}} is checked out by worktree {{.worktre
Error message
Branch {{.branchName}} is checked out by worktree {{.worktreeName}} What it means
Thrown by WorktreeHelper.newWorktreeForPickerValue when the user types a branch name (verbatim) that is already checked out by an existing worktree. Git refuses to check out the same branch in two worktrees, so lazygit blocks the operation up front. The branches-panel picker filters such branches out, but a free-form type-in bypasses that filter, hence this explicit guard.
Source
Thrown at pkg/gui/controllers/helpers/worktree_helper.go:84
})
return nil
}
// newWorktreeForPickerValue classifies the value the user picked or typed in the
// worktrees-panel picker and routes to the matching creation flow:
// - an existing local branch -> a worktree that checks it out;
// - a remote branch -> a new local tracking branch + worktree;
// - anything else -> a new branch off the current ref + worktree.
//
// All three then feed the shared location menu. The picker filters out branches
// already checked out somewhere, but a verbatim type-in is still guarded here.
func (self *WorktreeHelper) newWorktreeForPickerValue(value string) error {
if branch, ok := lo.Find(self.c.Model().Branches, func(branch *models.Branch) bool {
return branch.Name == value
}); ok {
if worktree, ok := git_commands.WorktreeForBranch(branch, self.c.Model().Worktrees); ok {
return errors.New(utils.ResolvePlaceholderString(self.c.Tr.BranchCheckedOutByWorktree,
map[string]string{"branchName": branch.Name, "worktreeName": worktree.Name}))
}
prompt := utils.ResolvePlaceholderString(self.c.Tr.WorktreeLocationPromptCheckout,
map[string]string{"branchName": branch.Name})
return self.promptForWorktreeLocation(branch.Name, prompt, func(path string) error {
return self.createWorktree(git_commands.NewWorktreeOpts{Path: path, Base: branch.RefName()}, context.WORKTREES_CONTEXT_KEY)
})
}
if _, branchName, ok := self.refsHelper.ParseRemoteBranchName(value); ok {
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)
})
}
View on GitHub (pinned to c477a2959b)
Solutions
- Switch to the existing worktree that already has the branch checked out (its name is in the error message) instead of creating a new one.
- Create the new worktree from a different base or a new branch name rather than the checked-out branch.
- If the existing worktree is stale, remove it first (worktrees panel), then retry.
- As a developer embedding lazygit's helper, pre-filter candidate branch names against Model().Worktrees using git_commands.WorktreeForBranch before calling the flow.
Example fix
// before
value := "feature-branch" // already checked out in ../wt-feature
err := helper.newWorktreeForPickerValue(value)
// after
if wt, ok := git_commands.WorktreeForBranch(branch, model.Worktrees); ok {
return helper.Switch(wt, context.WORKTREES_CONTEXT_KEY)
}
err := helper.newWorktreeForPickerValue(value) Defensive patterns
Strategy: validation
Validate before calling
func branchAvailableForWorktree(branch *models.Branch, worktrees []*models.Worktree) bool {
_, taken := git_commands.WorktreeForBranch(branch, worktrees)
return !taken
} Prevention
- Filter the branch picker against Model().Worktrees with git_commands.WorktreeForBranch before offering entries.
- Show which worktree holds each branch in the UI so users self-select correctly.
- When scripting worktree creation, verify the branch is not checked out anywhere first.
When it happens
Trigger: Calling the new-worktree flow with a value string that exactly matches a local branch for which git_commands.WorktreeForBranch(branch, model.Worktrees) returns a worktree. Typically: NewWorktree -> type a branch name that is already checked out in another worktree instead of picking from the filtered list.
Common situations: User forgets a branch is checked out in a long-lived worktree; stale worktree list after a worktree was removed outside lazygit; scripting the worktree creation flow with a hardcoded branch name.
Related errors
- Lazygit does not support bare repos.
- You are already in the selected worktree
- You cannot remove the main worktree!
- You cannot remove the current worktree!
- Git version must be at least %s. Please upgrade your git ver
AI-assisted analysis of jesseduffield/lazygit@c477a2959b (2026-08-15).
Data as JSON: /api/errors/3e9d64234ab7aeb2.
Report an issue: GitHub.