Skip to content

Checkout file from commit lacks confirmation when local changes exist #5142

@KJ7LNW

Description

@KJ7LNW

Description

When checking out a file from a commit (via commit files view, pressing 'c'), lazygit does not warn or confirm when the local working copy has uncommitted modifications. This can result in accidental data loss.

Steps to Reproduce

  1. View a commit (press Enter on commit in commits view)
  2. Navigate to the commit files panel
  3. Select a file that has local uncommitted modifications
  4. Press 'c' (checkout file)
  5. File is immediately overwritten without confirmation

Expected Behavior

Similar to hard reset operations (PR #4704), checkout should:

  • Detect if the target file has uncommitted changes
  • Show confirmation dialog before overwriting
  • Use the ConfirmIf pattern already established in the codebase

Actual Behavior

File is immediately overwritten without warning, losing local uncommitted changes.

Code Location

pkg/gui/controllers/commits_files_controller.go:263-272

The checkout function directly calls CheckoutFile without any confirmation check.

Suggested Fix

Apply the same ConfirmIf pattern used for hard reset in refs_helper.go:268:

func (self *CommitFilesController) checkout(node *filetree.CommitFileNode) error {
    fileHasChanges := /* check if node.GetPath() has uncommitted changes */
    
    return self.c.ConfirmIf(fileHasChanges,
        types.ConfirmOpts{
            Title:  self.c.Tr.Actions.CheckoutFile,
            Prompt: self.c.Tr.CheckoutFileConfirmation,
            HandleConfirm: func() error {
                self.c.LogAction(self.c.Tr.Actions.CheckoutFile)
                _, to := self.context().GetFromAndToForDiff()
                if err := self.c.Git().WorkingTree.CheckoutFile(to, node.GetPath()); err != nil {
                    return err
                }
                self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
                return nil
            },
        })
}

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions