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

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

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

这似乎还是有些过分。

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


当前回答

如果你的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

其他回答

我使用curl,它与公共回购或通过web界面使用https基本身份验证的人一起工作。

curl -L——retry 20——retry-delay 2 -O https://github.com/ACCOUNT/REPO/raw/master/PATH/TO/FILE/FILE.TXT -u USER:PASSWORD

我在github和bitbucket上进行了测试,两者都适用。

如果有web界面部署(如gitweb, cgit, Gitorious, ginatra),你可以使用它来下载单个文件('raw'或'plain'视图)。

如果其他方启用了它,你可以使用git存档的'——remote=<URL>'选项(可能限制它到给定文件所在的目录),例如:

$ git archive --remote=git@github.com:foo/bar.git --prefix=path/to/ HEAD:path/to/ |  tar xvf -

在我看来,这是一个解决方案:http://gitready.com/intermediate/2009/02/27/get-a-file-from-a-specific-revision.html

git show HEAD~4:index.html > local_file

其中4表示从现在开始的四次修订,~是注释中提到的波浪号。

如果你的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

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

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

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