859

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.

5
  • 19
    All the answers missed the fact you need to specify the full path of the file with git show: stackoverflow.com/questions/610208/… and stackoverflow.com/questions/2364147/…
    – VonC
    Oct 21, 2011 at 23:53
  • 27
    These days, you can use git show a1b35:./file.txt to use relative paths. Aug 12, 2013 at 4:57
  • 3
    @VonC - thank goodness for branch-aware tab completion :)
    – Luke
    May 18, 2017 at 11:00
  • more interesting could be showing differences git diff <other_branch> <path> Nov 20, 2019 at 10:07
  • 1
    The relative file example above with "a1b35", that is the branch name -- so of course put your own branch name there.
    – loneRanger
    Mar 9, 2020 at 16:02

5 Answers 5

1193

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.

9
  • 41
    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 - Jul 16, 2014 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, 2014 at 20:41
  • 5
    @GregBurghardt what works for me is something similar to vim -c "set syntax=html" -
    – raphaëλ
    Nov 13, 2014 at 10:54
  • 2
    This ONLY shows local branches, it does NOT show all branches on server Feb 16, 2017 at 19:58
  • 4
    @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, 2018 at 20:20
90
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
1
  • 1
    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. Feb 3, 2021 at 21:45
46

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.

9
  • 3
    This command results "git: 'gui' is not a git command. See 'git --help'. I'm a newbie; what's going on?
    – SMBiggs
    Nov 6, 2015 at 14:42
  • 3
    It actually works (to my surprise), see git-scm.com/docs/git-gui - I use git version 2.5.2.windows.1
    – anhoppe
    Jul 4, 2016 at 14:27
  • This answer was very helpful! Thanks. Should mention that im using git bash on windows.
    – merger
    Mar 9, 2017 at 13:11
  • Is it possible to use ranger as git gui?
    – acgtyrant
    Jun 29, 2018 at 7:12
  • 1
    @ScottBiggs you are not a newbie, you are using a nonstandard setup.
    – Ashnur
    Mar 11, 2019 at 13:31
24

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).

3
  • 1
    Is there a way to get this in Vim? (I'm open to plug-ins.)
    – NHDaly
    Feb 2, 2016 at 22:24
  • timeless advice ! Dec 30, 2022 at 18:14
  • 1
    @NHDaly vim-fugitive can do it: :Git show branch:path/file will open it in a new window.
    – datashaman
    Apr 9 at 11:36
13

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.

1
  • 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 1, 2020 at 7:29

Your Answer

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

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