I have a local git repository with a certain git commit checked out. A running emacs instance has an open file inside one of the buffers.

How do I quickly open the same file inside a new buffer, but on a different commit?

The use case is to have a clear view on the changes between the two commits on that file.

  • VC is underrated
    – Omar
    Oct 23 '21 at 1:15

The VC package is built into Emacs and provides some basic support for browsing file versions, browsing logs, checking out, committing, etc. VC commands are bound to C-x v by default.

There are several commands to access other revisions. If you know what revision you want to open, press C-x v ~ (vc-revision-other-window). To open the last revision that changed a given line, press C-x v g (vc-annotate), and press f (vc-annotate-find-revision-at-line) on the desired line. If you want to find and open a revision based on its log message, run C-x v l (vc-print-log) and press f (log-view-find-revision) when the cursor is on the desired commit.

If you're interested in the changes between two commits, you can directly show a diff with C-u C-x v = (vc-diff with a prefix argument — by default it compares the current content of the file with the committed version). To see the differences in Emacs's Ediff interface, run M-1 M-x vc-ediff RET (vc-ediff with a prefix argument). You can also view a diff with the current content quickly from the annotate or log buffer with d, but there's no convenient access to Ediff from there. A convenient way to reach Ediff is to arrange for both versions to be open, and run ediff-buffers, which prompts for the buffers to compare and defaults to the current buffer and the previously selected buffer.

The external package Magit has a much more complete interface to Git than VC. It's very useful for interfacing with Git, but not particularly helpful when you're focused on one file in particular and not checking out or committing.


Please see the following StackOverflow duplicate Q&A.


(I won't quote it to avoid splintering answers across two sites on the same network, which is counter-productive.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.