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

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

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

这似乎还是有些过分。

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


当前回答

这是特定于托管在GitHub上的git回购

尝试Github的命令行应用程序gh的“api”命令,对Github的“获取存储库内容”端点进行身份验证调用。

基本命令是:

$gh api /repos/{owner}/{repo}/contents/<path_to_the_file>

作为额外的奖励,当您从包含您试图从中获取文件的repo副本的目录中执行此操作时,{owner}和{repo}部分将自动填充。

https://docs.github.com/en/rest/reference/repos#get-repository-content

响应将是一个JSON对象。如果<path_to_the_file>确实指向一个文件,JSON将包括一个'size', 'name',几个访问文件的url字段,以及一个'content'字段,这是文件内容的base64编码版本。

要获得文件内容,你可以卷曲“download_url”的值,或者只是解码“content”字段。你可以通过管道base64命令来做到这一点,就像这样:

$gh api /repos/{owner}/{repo}/contents/<path-to-the-file> --jq '.content' | base64 -d

其他回答

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

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

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

我用这个

$ 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'

对于单个文件,只需使用wget命令。

首先,按照下面的图片点击“raw”来获取url,否则你将下载嵌入html中的代码。

然后,浏览器将打开一个新页面,url以https://raw.githubusercontent.com/开头…

只需在终端输入命令:

#wget https://raw.githubusercontent.com/...

一会儿文件就会放在你的文件夹里。

使用实例远程导出单个文件。

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

这将下载文件README。Md到当前目录。

如果你想把文件的内容导出到STDOUT:

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

可以在命令末尾提供多个路径。

在我看来,使用以下方法是最简单的:

wget https://github.com/name/folder/file.zip?raw=true