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

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

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

这似乎还是有些过分。

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


当前回答

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

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

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

只需在终端输入命令:

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

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

其他回答

我是这样解决的:

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"

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

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

在我看来,这是一个解决方案: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

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

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

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

只需在终端输入命令:

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

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