Magit revert single file

GitEmacsMagit

Git Problem Overview


The question is about Magit major mode for Emacs function and less so about how to do this via command line interface.

I only have a local Git repository. How would I restore a selected file to its previous version? I believe that the Git command for this must be something like git checkout HEAD^ path/to/file but I may be wrong here too, just guessing really.

What I'm looking for is essentially the same thing as C-x v u in vc-dir buffer.

Essentially, what I was hoping to do was:

  • delete the modified file.

  • pull from local repo.

But Magit doesn't seem to be able to do that, it seems to prefer to just delete the file, instead of restoring it.

Git Solutions


Solution 1 - Git

In magit you just have to go on the hunk or file you want to revert, and use k to discard a modification. See the relevant documentation at https://magit.vc/manual/1.4/magit/Staging-and-Committing.html for more detail.

Solution 2 - Git

With the file open, you can M-x magit-file-checkout.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser797257View Question on Stackoverflow
Solution 1 - GitRémiView Answer on Stackoverflow
Solution 2 - GitRazzi AbuissaView Answer on Stackoverflow