I accidentally pressed "k" (a magit command) and discarded some unstaged changes. Is there a way to undo this? Or did I discover one of the few ways to actually lose work while using git?
As mention in the comments, if, at the time you discard a change, a buffer is visiting the file that is modified by the discard, then you can simply go to that buffer and undo the change.
The comments also mention that Magit could create a commit whenever it performs an action that destroys data that could otherwise not be recovered. Turns out Magit has been doing that for years, but only if told to do so.
This is implemented using several minor-modes that can be enabled individually. To enable them all use this.
(use-package magit-wip :after magit :config (magit-wip-before-change-mode) (magit-wip-after-apply-mode) (magit-wip-after-save-mode))
You should also add this line at the top-level (not inside
(add-hook 'before-save-hook 'magit-wip-commit-initial-backup)
Some other related settings:
(use-package magit :config (setq magit-wip-merge-branch t) (magit-define-popup-action 'magit-log-popup ?i "Index wipref" 'magit-wip-log-index) (magit-define-popup-action 'magit-log-popup ?w "Worktree wipref" 'magit-wip-log-worktree))
Most of this is documented in the manual. Also see the doc-string of