68

Is it possible to open a specific revision of a file in Emacs with Magit without changing the current branch?

I have rebased some local changes to a revision where the modified functions were replaced by functionality in a different file. I would like to open the old revision of my changes in a separate buffer so I can compare the changes side by side.

My current hacky solution has been to run git show <branch pre-rebase>:file > old_file and then open old_file in Emacs.

David Holm
  • 1,179
  • 1
  • 10
  • 10
  • 1
    https://github.com/pidu/git-timemachine can view a file between each versions in a glance temporarily. – kuanyui Jan 23 '15 at 09:27
  • 1
    @kuanyui I use `git-timemachine` but since my code has been rebased I don't think it will work since the old code is not part of the current branch's history? – David Holm Jan 23 '15 at 09:35
  • Duplicate of http://stackoverflow.com/questions/25420282/using-emacs-and-magit-to-visit-a-file-in-given-commit-branch-etc – phils Jan 25 '15 at 08:42

2 Answers2

90

You can view a file for a specific revision using M-x magit-find-file RET <filename> RET.

Or you can first view the diff for commit by pressing RET on it, navigating to the file inside the diff, and then pressing RET again.

tarsius
  • 25,298
  • 4
  • 69
  • 109
Kyle Meyer
  • 6,914
  • 26
  • 22
  • 15
    This works for me; here's what I do: from the current version of the file, `M-x magit-log-buffer-file`, move the line to the desired commit, then `M-x magit-find-file`. Then the default value will be that commit. I do have to give the path to the file as this is not picked up from the log buffer. – Liam Dec 11 '15 at 17:31
  • @tarsius, Somewhat like magit-find-file but where I only need to select the revision for the current buffer's file. Does it exist already? – ergosys Aug 13 '18 at 18:59
  • Yes, use `magit-find-file` itself from a file-visiting buffer and then just type `RET` at the second prompt (the proper file is offered as the default). – tarsius Aug 13 '18 at 22:40
  • Thanks for the info and all all your work on Magit. It wasn't too difficult to hack a function that skips that pesky extra keystroke, so I'm happy until the next itch occurs. – ergosys Aug 14 '18 at 05:25
40

It's possible to do with plain VC (built-in to Emacs), without Magit. Type C-x v ~ and type the version you want. It understands tags and SHA1 hashes, as well as the usual ~1 etc suffixes.

legoscia
  • 6,012
  • 29
  • 54