70

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

2 Answers 2

92

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
  • 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, 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?
    – ergosys
    Aug 13, 2018 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, 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.
    – ergosys
    Aug 14, 2018 at 5:25
41

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?
    – Bryan Ash
    Sep 18, 2016 at 19:59
  • 13
    @BryanAsh yes, if the solution in C is so much easier. Apr 22, 2018 at 19:31
  • 3
    Brilliant, exactly what I needed, it may be off-topic but this answer certainly does the job!
    – guychouk
    Dec 15, 2019 at 18:26
  • 1
    Cross-referencing with stackoverflow.com/q/25420282
    – phils
    May 27, 2020 at 3:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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