Home > Software engineering >  How do I open a file from another Git branch in Emacs? [duplicate]
How do I open a file from another Git branch in Emacs? [duplicate]

Time:10-08

Working in BranchA, I frequently need to refer to a file in BranchB. The git show BranchB:path/to/file command will dump the contents of file to the terminal, but that's hard to read and navigate. I'd like to have BranchB:path/to/file open in an Emacs window.

The top comment on this answer indicates that I can do this with vim by git show branch:file | vim -. If I try this with Emacs,

git show BranchB:path/to/file | emacs -

an empty Emacs buffer opens with a message "Unknown option '-'" in the minibuffer.

I know that I can send the output of git show to a file and then open that file in Emacs, but I'm looking for a more streamlined workflow that I expect exists.

How can I open a file from another Git branch directly in Emacs?

CodePudding user response:

This is a cross-site duplicate of superuser's How to make Emacs read buffer from stdin on start?. The answer there works here:

emacs --insert <(git show BranchB:path/to/file)
  • Related