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?

  • Have you tried using undo in the affected buffers? (Assuming you had them open.)
    – phils
    Commented Oct 2, 2018 at 0:39
  • Note that if the changes were never staged then you weren't "using git" so to speak -- git never knew about those changes.
    – phils
    Commented Oct 2, 2018 at 0:41
  • @phils great suggestion. I didn't think of this during my panic, unfortunately.
    – truthling
    Commented Oct 2, 2018 at 20:44
  • @phils git knew about them, but only in a general sense, i.e. git knew there were unstaged changes to files it was tracking. And it was a git command that discarded those changes. But your point is well taken.
    – truthling
    Commented Oct 2, 2018 at 20:47
  • Right, although it's more that git will figure out on demand if there are unstaged changes (and then, I believe, immediately forget again). Unless it's told to store the changeset in some way, it won't remember it.
    – phils
    Commented Oct 2, 2018 at 22:25

1 Answer 1


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

You should also add this line at the top-level (not inside use-package):

(add-hook 'before-save-hook 'magit-wip-commit-initial-backup)

Some other related settings:

(use-package magit
  (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 magit-wip-merge-branch.

  • Since magit now uses transient rather than magit-popup, the last "other settings" bit doesn't work directly, but throws a warning. Here's alternative code to fix it: ``` (use-package magit :config (setq magit-wip-merge-branch t) (transient-append-suffix 'magit-log "a" '("i" "Index wipref" magit-wip-log-index)) (transient-append-suffix 'magit-log "i" '("w" "Worktree wipref" magit-wip-log-worktree))) ``` Commented Jul 22, 2019 at 18:14
  • 1
    "i" and "w" are now automatically added to magit-log when magit-wip-mode is turned on. And it is discouraged to used the individual modes. So just (magit-wip-mode 1) should do.
    – Hubisan
    Commented Nov 4, 2019 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.