33

I figured out I can show old versions of a file using 'git log filename' to show the commits, and then use 'git show commit-id:filename' for the old version. But it just puts it in less.

I would like to be able to view this in emacs, so I can navigate as I'm used to and so that there is syntax highlighting. I found out I can't set the git core.pager to emacs since emacs cannot read from stdin.

Does anyone know how I could do this? Or do you have another good way of checking old versions of files?

kodu
  • 2,176
  • 3
  • 17
  • 22
  • 7
    I *do* have a better way of checking old revisions: with vim :p. A simple `git show commit-id:filename | vim -` and you're done! – jeanpaul62 Aug 09 '13 at 14:02
  • You can also try `most` as pager, it has color support. – Mali Aug 09 '13 at 15:06
  • Better answers available at http://stackoverflow.com/questions/25420282/using-emacs-and-magit-to-visit-a-file-in-given-commit-branch-etc - in particular, `C-x v ~` is great for switching branches too. – Ibrahim Dec 23 '15 at 06:33
  • Also if your intent is debugging and tracing a bug by peeking past commits, you can use `git bisect`. – aderchox Jun 02 '22 at 18:22

4 Answers4

38

Just use > and put it into a file that you can open in emacs.

git show commit-id:filename > oldfile

Then open the file in emacs.

Schleis
  • 41,516
  • 7
  • 68
  • 87
9

If you are using bash, you can use Process Substitution.

gvim <(git show commit-id:filename)
Nimlar
  • 655
  • 7
  • 15
  • When emacs opens it says 'symbolic link to nonexistent file' in the minibuffer and an empty buffer is shown – kodu Nov 14 '13 at 09:07
  • I didn't test `emacs <(git show commit-id:filename)`, I am a vim user.... And this command doesn't work with emacs: when emacs opens the temporary file created by the `<( )` command, the file doesn't exist anymore... – Nimlar Nov 19 '13 at 15:55
7

Using the following Vim command, one can view a previous version of a file without having to cleanup anything afterward.

 git show commit-id:filename | vim - -n

Explanation: The dash argument of the vim command makes vim load whatever comes in from standard input. The -n option suppresses the creation of swap files.

3

Emacs package git-timemachine lets you step back and forth through git revisions of a file:

https://github.com/pidu/git-timemachine

Peter
  • 31
  • 1