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

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

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

这似乎还是有些过分。

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


当前回答

直接从浏览器(我使用safari…)右键单击“查看原始”,选择“下载链接文件”:

其他回答

我用这个

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

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

Yisrael Dov的答案很简单,但它不允许压缩。您可以使用——format=zip,但不能像使用tar一样使用管道命令直接解压缩,因此需要将其保存为临时文件。这是一个脚本:

#!/bin/bash

BASENAME=$0

function usage {
    echo "usage: $BASENAME <remote-repo> <file> ..."
    exit 1
}

[ 2 -gt "$#" ] && { usage; }

REPO=$1
shift
FILES=$@

TMPFILE=`mktemp`.zip
git archive -9 --remote=$REPO HEAD $FILES -o $TMPFILE
unzip $TMPFILE
rm $TMPFILE

这也适用于目录。

我是这样解决的:

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"

如果没有其他答案工作(即限制GitLab访问),您可以通过以下方式进行“选择性签出”:

git clone—no-checkout—depth=1—no-tags URL git恢复——分级的DIR-OR-FILE git校验DIR-OR-FILE

尽管这个解决方案100%兼容git,并且您可以签出目录,但与对文件执行wget/curl相比,它在磁盘和网络上都不是最优的。