从远程git存储库获取单个文件内容的最有效机制(就传输的数据和使用的磁盘空间而言)是什么?

到目前为止,我已经想出了:

git clone --no-checkout --depth 1 git@github.com:foo/bar.git && cd bar && git show HEAD:path/to/file.txt

这似乎还是有些过分。

从回购中获取多个文件呢?


当前回答

下面是Jakub的回答。Git存档生成tar或zip存档,所以你需要通过tar管道输出以获得文件内容:

git archive --remote=git://git.foo.com/project.git HEAD:path/to/directory filename | tar -x

将在当前目录中保存远程存储库HEAD中的'filename'的副本。

:path/to/directory部分是可选的。如果排除,提取的文件将保存到<当前工作目录>/path/to/directory/filename

此外,如果你想在git-daemon托管的git存储库上启用git archive——remote,你需要启用守护进程。Uploadarch配置选项。参见https://kernel.org/pub/software/scm/git/docs/git-daemon.html

其他回答

如果你的Git存储库托管在Azure-DevOps (VSTS)上,你可以使用Rest API检索单个文件。

这个API的格式是这样的:

 https://dev.azure.com/{organization}/_apis/git/repositories/{repositoryId}/items?path={pathToFile}&api-version=4.1?download=true

例如:

 https://dev.azure.com/{organization}/_apis/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249/items?scopePath=/MyWebSite/MyWebSite/Views/Home/_Home.cshtml&download=true&api-version=4.1

我用这个

$ cat ~/.wgetrc
check_certificate = off

$ wget https://raw.github.com/jquery/jquery/master/grunt.js
HTTP request sent, awaiting response... 200 OK
Length: 11339 (11K) [text/plain]
Saving to: `grunt.js'

这里有一些答案的微妙变化,回答了OP的问题:

git archive --remote=git@archive-accepting-git-server.com:foo/bar.git \
  HEAD path/to/file.txt | tar -xO path/to/file.txt > file.txt

我是这样解决的:

git archive --remote=ssh://git@gitlab.com/user/mi-repo.git BranchName /path-to-file/file_name | tar -xO /path-to-file/file_name > /path-to-save-the-file/file_name

如果你愿意,你可以用"BranchName"代替"HEAD"

在git版本1.7.9.5中,这似乎可以从远程导出单个文件

git archive --remote=ssh://host/pathto/repo.git HEAD README.md | tar xO

这将包含文件README.md的内容。