712

Is it possible to open a file in a git branch without checking out that branch? How?

Essentially I want to be able to open a file in my github pages branch without switching branches all the time. I don't want to modify it, just want to view it.

Gilles 'SO- stop being evil'
  • 92,660
  • 35
  • 189
  • 229
Schneems
  • 13,095
  • 8
  • 50
  • 79
  • 16
    All the answers missed the fact you need to specify the full path of the file with `git show`: http://stackoverflow.com/questions/610208/how-to-retrieve-a-single-file-from-specific-revision-in-git/610315#610315 and http://stackoverflow.com/questions/2364147/how-to-get-just-one-file-from-another-branch/2364223#2364223 – VonC Oct 21 '11 at 23:53
  • 18
    These days, you can use `git show a1b35:./file.txt` to use relative paths. – Steve Bennett Aug 12 '13 at 04:57
  • 2
    @VonC - thank goodness for branch-aware tab completion :) – Luke May 18 '17 at 11:00
  • more interesting could be showing differences `git diff ` – Sławomir Lenart Nov 20 '19 at 10:07
  • The relative file example above with "a1b35", that is the branch name -- so of course put your own branch name there. – loneRanger Mar 09 '20 at 16:02

5 Answers5

993

This should work:

git show branch:file

Where branch can be any ref (branch, tag, HEAD, ...) and file is the full path of the file. To export it you could use

git show branch:file > exported_file

You should also look at VonC's answers to some related questions:

UPDATE 2015-01-19:

Nowadays you can use relative paths with git show a1b35:./file.txt.

Community
  • 1
  • 1
Scolytus
  • 14,109
  • 6
  • 39
  • 63
  • 28
    And you can pop the file open in vim as well: `git show branch:file | vim -` (Notice the "|" pipe, and the trailing dash after the vim command: `vim -` – Greg Burghardt Jul 16 '14 at 20:42
  • @GregBurghardt: That's cool. It would be cooler if I can somehow give vim a hint about how to highlight the content – GuLearn Jul 25 '14 at 20:41
  • I think if you do `vim -c "set syntax on" -` that should turn syntax highlighting on. Haven't tried it yet. – Greg Burghardt Jul 26 '14 at 00:43
  • 3
    @GregBurghardt what works for me is something similar to `vim -c "set syntax=html" -` – raphaëλ Nov 13 '14 at 10:54
  • @rparree vim guesses the correct syntax from the file suffix. You can avoid this by adding a vim modline as a comment to your files. That way you can also set more file-specific settings, i.e. indentation etc. http://vim.wikia.com/wiki/Modeline_magic – gschenk Apr 14 '16 at 04:00
  • After piping into vim just press the colon key and type `set syntax=python` or whatever :) – rjh Apr 23 '16 at 11:52
  • 2
    I wish I could upvote this answer more than once, honestly. – Vincent Fourmond Jan 22 '17 at 13:44
  • 2
    This ONLY shows local branches, it does NOT show all branches on server – William Entriken Feb 16 '17 at 19:58
  • 3
    @FullDecent, I was able to use ```origin/my_remote_branch``` fine with this. Or do you mean the actual server version? If you want that, you just need to ```git fetch``` first. – rsmith54 Jan 25 '18 at 20:20
  • if you are in Vim, you can do `%!git show :%` that will open the git show result in a buffer, – Renan Cidale Apr 13 '21 at 08:56
58
git show somebranch:path/to/your/file

you can also do multiple files and have them concatenated:

git show branchA~10:fileA branchB^^:fileB

You do not have to provide the full path to the file, relative paths are acceptable e.g.:

git show branchA~10:../src/hello.c

If you want to get the file in the local directory (revert just one file) you can checkout:

git checkout somebranch^^^ -- path/to/file
Stabledog
  • 2,787
  • 1
  • 27
  • 38
Adam Dymitruk
  • 109,813
  • 21
  • 138
  • 137
  • For Windows users: This is one of the few places where git bash for Windows doesn't know to normalise case and path separators. i.e. You must match the case exactly and use `/` instead of `\ ` - even though cmd's completion will try to tell you the opposite. – pullmyteeth Feb 03 '21 at 21:45
39

A simple, newbie friendly way for looking into a file: git gui browser <branch> which lets you explore the contents of any file.

It's also there in the File menu of git gui. Most other -more advanced- GUI wrappers (Qgit, Egit, etc..) offer browsing/opening files as well.

inger
  • 17,374
  • 9
  • 45
  • 51
23

If you're using Emacs, you can type C-x v ~ or M-x vc-revision-other-window to see a different revision of the file you're currently editing (tags, branches and hashes all work).

legoscia
  • 37,068
  • 22
  • 103
  • 148
7

Add the following to your ~/.gitconfig file

[alias]
  cat = "!git show \"$1:$2\" #"

And then try this

git cat BRANCHNAME FILEPATH

Personally I prefer separate parameters without a colon. Why? This choice mirrors the parameters of the checkout command, which I tend to use rather frequently and I find it thus much easier to remember than the bizarro colon-separated parameter of the show command.

akuhn
  • 25,901
  • 2
  • 72
  • 86
  • 1
    **Bash completions** works with git aliases immediately after `.git/config` was saved, e.g. save config and try `git cat ma[tab-tab] docke[tab-tab]`. Thx, @akuhn – viktorkho Jul 01 '20 at 07:29