If you have made extensive changes to a file and then change your mind about them, you can get rid of them by reading in the previous version of the file. To do this, use M-x revert-buffer, which operates on the current buffer. Since reverting a buffer unintentionally could lose a lot of work, you must confirm this command with yes.
revert-buffer
tries to position point in such a way that, if
the file was edited only slightly, you will be at approximately the
same piece of text after reverting as before. However, if you have made
drastic changes, point may wind up in a totally different piece of text.
Reverting marks the buffer as “not modified” until another change is made.
Some kinds of buffers whose contents reflect data bases other than files,
such as Dired buffers, can also be reverted. For them, reverting means
recalculating their contents from the appropriate data base. Buffers
created explicitly with C-x b cannot be reverted; revert-buffer
reports an error when asked to do so.
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, whenever you visit the file again with C-x C-f.
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, find-file
and 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 may find it useful to have Emacs revert files automatically when they change. Three minor modes are available to do this.
M-x global-auto-revert-mode runs Global Auto-Revert mode, which periodically checks all file buffers and reverts when the corresponding file has changed. M-x auto-revert-mode runs a local version, Auto-Revert mode, which applies only to the buffer in which it was activated. Auto-Revert mode can be used 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, you can tail the file more efficiently using Auto-Revert Tail mode, M-x auto-revert-tail-mode.
The variable auto-revert-interval
controls how often to check
for a changed file. Since checking a remote file is too slow, these
modes do not check or revert remote files.
See VC Mode Line, for Auto Revert peculiarities in buffers that visit files under version control.