If you have made extensive changes to a file-visiting buffer and then change your mind, you can revert the changes and go back to the saved version of the file. To do this, type
M-x revert-buffer. Since reverting unintentionally could lose a lot of work, Emacs asks for confirmation first.
revert-buffer command tries to position point in such a way that, if the file was edited only slightly, you will be at approximately the same part of the text as before. But if you have made major changes, point may end up in a totally different location.
Reverting marks the buffer as not modified. However, it adds the reverted changes as a single modification to the buffer’s undo history (see Undo). Thus, after reverting, you can type
C-/ or its aliases to bring the reverted changes back, if you happen to change your mind.
Some kinds of buffers that are not associated with files, such as Dired buffers, can also be reverted. For them, reverting means recalculating their contents. Buffers created explicitly with
C-x b cannot be reverted;
revert-buffer reports an error if you try.
When you edit a file that changes automatically and frequently—for example, a log of output from a process that continues to run—it may be useful for Emacs to revert the file without querying you. To request this behavior, set the variable
revert-without-query to a list of regular expressions. When a file name matches one of these regular expressions,
revert-buffer will revert it automatically if it has changed—provided the buffer itself is not modified. (If you have edited the text, it would be wrong to discard your changes.)
You can also tell Emacs to revert buffers automatically when their visited files change on disk; see Auto Revert.