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. It also clears the buffer’s undo history (see Undo). Thus, the reversion cannot be undone—if you change your mind yet again, you can’t use the undo commands to bring the reverted changes back.
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
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 periodically. To do this for a specific buffer, enable the minor mode Auto-Revert mode by typing M-x auto-revert-mode. This automatically reverts the current buffer every five seconds; you can change the interval through the variable auto-revert-interval. To do the same for all file buffers, type M-x global-auto-revert-mode to enable Global Auto-Revert mode. These minor modes do not check or revert remote files, because that is usually too slow.
One use of Auto-Revert mode is to “tail” a file such as a system
log, so that changes made to that file by other programs are
continuously displayed. To do this, just move the point to the end of
the buffer, and it will stay there as the file contents change.
However, if you are sure that the file will only change by growing at
the end, use Auto-Revert Tail mode instead
auto-revert-tail-mode). It is more efficient for this.
Auto-Revert Tail mode works also for remote files.
See VC Undo, for commands to revert to earlier versions of files under version control. See VC Mode Line, for Auto Revert peculiarities when visiting files under version control.