jesseduffield/lazygit · warning

You cannot remove the main worktree!

Error message

You cannot remove the main worktree!

What it means

Returned by WorktreesController.remove when the selected models.Worktree has IsMain == true. The main worktree hosts the repository's .git directory; removing it would destroy the repo, so lazygit refuses unconditionally before even showing the removal menu.

Source

Thrown at pkg/gui/controllers/worktrees_controller.go:127

		}

		self.c.RenderToMainViews(types.RefreshMainOpts{
			Pair: self.c.MainViewPairs().Normal,
			Main: &types.ViewUpdateOpts{
				Title: self.c.Tr.WorktreeTitle,
				Task:  task,
			},
		})
	}
}

func (self *WorktreesController) add() error {
	return self.c.Helpers().Worktree.NewWorktree()
}

func (self *WorktreesController) remove(worktree *models.Worktree) error {
	if worktree.IsMain {
		return errors.New(self.c.Tr.CantDeleteMainWorktree)
	}

	if worktree.IsCurrent {
		return errors.New(self.c.Tr.CantDeleteCurrentWorktree)
	}

	removeWorktreeItem := &types.MenuItem{
		Label: self.c.Tr.RemoveWorktree,
		Keys:  menuKey('w'),
		OnPress: func() error {
			return self.c.Helpers().Worktree.Remove(worktree, nil)
		},
	}

	branch, branchFound := lo.Find(self.c.Model().Branches, func(branch *models.Branch) bool {
		return branch.Name == worktree.Branch
	})
	// A worktree with a detached HEAD has no branch to delete

View on GitHub (pinned to c477a2959b)

Solutions

  1. Select a linked worktree instead — the main worktree cannot be removed by design.
  2. To delete the whole repository, remove the directory from the filesystem outside lazygit.
  3. If the main worktree entry looks wrong (stale), run 'git worktree prune' and refresh.
Defensive patterns

Strategy: validation

Validate before calling

if worktree.IsMain {
    // hide or disable the remove action for the main worktree
}

Prevention

When it happens

Trigger: Pressing the remove/delete key on the main worktree entry in the worktrees panel (worktree.IsMain set, typically the entry whose path is the repo root).

Common situations: User wants to delete 'everything' and selects the first/main entry; unfamiliarity with which entry is main; cleanup scripts iterating all worktrees.

Related errors


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