Retrieve a single file from a repository
What is the most efficient mechanism (in respect to data transferred and disk space used) to get the contents of a single file from a remote git repository?
So far I've managed to come up with:
git clone --no-checkout --depth 1 git@github.com:foo/bar.git && cd bar && git show HEAD:path/to/file.txt
This still seems overkill.
What about getting multiple files from the repo?
in git version 1.7.9.5 this seems to work to export a single file from a remote
git archive --remote=ssh://host/pathto/repo.git HEAD README.md
This will cat the contents of the file README.md
.
If there is web interface deployed (like gitweb, cgit, Gitorious, ginatra), you can use it to download single file ('raw' or 'plain' view).
If other side enabled it, you can use git archive 's ' --remote=<URL>
' option (and possibly limit it to a directory given file resides in), for example:
$ git archive --remote=git@github.com:foo/bar.git --prefix=path/to/ HEAD:path/to/ | tar xvf -
Following on from Jakub's answer. git archive
produces a tar or zip archive, so you need to pipe the output through tar to get the file content:
git archive --remote=git://git.foo.com/project.git HEAD:path/to/directory filename | tar -x
Will save a copy of 'filename' from the HEAD of the remote repository in the current directory.
The :path/to/directory
part is optional. If excluded, the fetched file will be saved to <current working dir>/path/to/directory/filename
In addition, if you want to enable use of git archive --remote
on Git repositories hosted by git-daemon, you need to enable the daemon.uploadarch config option. See https://kernel.org/pub/software/scm/git/docs/git-daemon.html
上一篇: 删除远程存储库上不再存在的本地git标签
下一篇: 从存储库中检索单个文件