25

I am looking for the equivalent of svn cat in git.

Yes, I am aware that the similar question was asked here. The answer is to use git show rev:path.

However, svn cat can be used for the remote repository. That is, I can do svn cat url@rev and get the file from the specified revision of the remote repository, without getting the whole repository. My understanding is that git show only applies to the local repository.

A workaround I found is to use gitweb interface to get the blob.

Community
  • 1
  • 1
sanxiyn
  • 3,648
  • 1
  • 19
  • 15
  • 3
    I don't think there's any way to do this without downloading more than you want: the git server protocol doesn't make it available. Aside from downloading (a part of) a remote repository, the only other thing I think you can do is get remote ref information (git ls-remote). – Nicholas Riley May 15 '10 at 05:38
  • 2
    I would agree with Nicholas: I think the only way to do this is to "git fetch" (which will update your repository, but NOT merge anything in), then run one of the commands in the mentioned thread. – RyanWilcox May 16 '10 at 16:03
  • 1
    or git remote update (git fetch for all remotes) – xenoterracide May 28 '10 at 07:17
  • Do you use github? Since I have a solution for you if so. – Restuta Nov 16 '16 at 01:50

4 Answers4

15

Here is a hack you can use with recent versions of Git to get a single file from a remote repo (to which you have Git access) without cloning the repo:

git archive --remote=git@gitserver.example.com:myrepo master path/to/file1 | tar -xOf - > file1

The above gets the file from master. Modify to your needs.

ravi
  • 3,319
  • 4
  • 19
  • 11
12
git fetch
git show remotes/origin/master:<filename>

Obviously your remote branch might not be at remotes/origin/master.

Robin Winslow
  • 10,908
  • 8
  • 62
  • 91
11

If your motivation is to minimize the amount of data downloaded and/or stored on your local disk, you could try the below commands.

git clone --depth=1 --bare REPO
cd REPO.git
git show HEAD:PATH/TO/FILE

--depth=1 means you'll download only the most recent revision, not any history. --bare means you will download the remote repository, but won't actually create any working tree, saving space on your local disk.

Note that if you're interested in a version of the file other than the most recent, the above commands won't work, since they do not download any history.

Russell Silva
  • 2,772
  • 3
  • 26
  • 36
3

In another thread it is mentioned that it is impossible to fetch a single file. So the only options are to either fetch the remote repository, or look at the file with another tool (like a web browser for example).

Github has excellent support for viewing files online, you could integrate that with curl if you like to stay on the commandline. For other repositories, there might not be such features.

Community
  • 1
  • 1
iwein
  • 25,788
  • 10
  • 70
  • 111