53

Is there a way to issue the command:

git checkout NameOfFile

on a modified and unstaged file from within MAGIT?

3
  • 3
    You can press k to kill the changes when point is on the same line with the unstaged file. Commented May 26, 2015 at 16:34
  • 3
    Try x when hovering a hunk in spacemacs. Commented Oct 12, 2018 at 13:20
  • @moritzschaefer There is no indication that this question applies to spacemacs. Commented Oct 19, 2018 at 16:39

2 Answers 2

62

You can use one of the below to discard changes in unstaged files:

  • M-x magit-revert-item (bound to v in magit-status-mode)
  • M-x magit-discard-item (bound to k in magit-status-mode) - works on staged items too

Using either method will ask you to confirm before discarding.

11
  • 4
    You can also use k which is bound to magit-discard-item in magit-mode. Commented May 26, 2015 at 16:35
  • Yes, that works too. Commented May 26, 2015 at 16:36
  • Tried using both methods, however I still receive the message: "Nothing to discard here". Commented Jun 12, 2015 at 15:55
  • thanks! Magit-mode seems like the best way to do just about everything I've found... Commented Apr 9, 2016 at 16:32
  • 1
    What's incredible is that k and v also work on local changes inside a file! You can thus reverse some changes and keep the others in the same file. Magit rocks \o/ Commented May 29, 2020 at 8:21
21

Reversing, reverting and discarding are distinct actions. Together with staging and unstaging, they are referred to as "apply variants".

To discard a change means to throw it away. Only un-committed changes can be discarded. When a staged change is discarded it is not only removed from the index (that is known as unstaging), but also from the work tree.

To reverse a change means to apply it to the work tree in reverse. Both committed and merely staged changes can be reversed. (Reversing staged changes is actually useful, i.e. when splitting up a commit).

Only commits can be reverted, in other regards it's similar to reversing.

See https://github.com/magit/magit/wiki/History-Manipulation for details.

2
  • "Reverting staged changes..." - you meant "Reversing staged changes..." I guess Commented May 27, 2015 at 13:41
  • Yes. Common mistake because this distinction is not as clear as the other :-) Commented May 27, 2015 at 14:27

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.