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 reposito开发者_运维知识库ry, 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.
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.
git fetch
git show remotes/origin/master:<filename>
Obviously your remote branch might not be at remotes/origin/master
.
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.
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.
精彩评论