Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View a file in a different Git branch without changing branches

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.

like image 695
Schneems Avatar asked Oct 21 '11 23:10

Schneems


People also ask

Does switching branches change files?

When you switch branches, files that are not tracked by Git will remain untouched. Since Git does not know about new_file. dat , it will not just delete it.

How do I pull only a specific file in git?

git checkout origin/master -- path/to/file // git checkout <local repo name (default is origin)>/<branch name> -- path/to/file will checkout the particular file from the downloaded changes (origin/master).


2 Answers

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:

  • How to retrieve a single file from specific revision in Git?
  • How to get just one file from another branch

UPDATE 2015-01-19:

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

like image 167
Scolytus Avatar answered Sep 19 '22 13:09

Scolytus


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 
like image 38
Adam Dymitruk Avatar answered Sep 18 '22 13:09

Adam Dymitruk