71

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.

3
  • 1
    github.com/pidu/git-timemachine can view a file between each versions in a glance temporarily. Commented Jan 23, 2015 at 9: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? Commented Jan 23, 2015 at 9:35
  • Duplicate of stackoverflow.com/questions/25420282/… Commented Jan 25, 2015 at 8:42

2 Answers 2

96

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.

4
  • 17
    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. Commented Dec 11, 2015 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? Commented Aug 13, 2018 at 18:59
  • 1
    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). Commented Aug 13, 2018 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. Commented Aug 14, 2018 at 5:25
42

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.

4
  • This is not an answer to a magit question. Would you post a C answer to a Ruby question? Commented Sep 18, 2016 at 19:59
  • 13
    @BryanAsh yes, if the solution in C is so much easier. Commented Apr 22, 2018 at 19:31
  • 3
    Brilliant, exactly what I needed, it may be off-topic but this answer certainly does the job! Commented Dec 15, 2019 at 18:26
  • 1
    Cross-referencing with stackoverflow.com/q/25420282 Commented May 27, 2020 at 3:49

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.