从远程git存储库获取单个文件内容的最有效机制(就传输的数据和使用的磁盘空间而言)是什么?
到目前为止,我已经想出了:
git clone --no-checkout --depth 1 git@github.com:foo/bar.git && cd bar && git show HEAD:path/to/file.txt
这似乎还是有些过分。
从回购中获取多个文件呢?
从远程git存储库获取单个文件内容的最有效机制(就传输的数据和使用的磁盘空间而言)是什么?
到目前为止,我已经想出了:
git clone --no-checkout --depth 1 git@github.com:foo/bar.git && cd bar && git show HEAD:path/to/file.txt
这似乎还是有些过分。
从回购中获取多个文件呢?
当前回答
在我看来,使用以下方法是最简单的:
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表示从现在开始的四次修订,~是注释中提到的波浪号。
下面是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
在我看来,使用以下方法是最简单的:
wget https://github.com/name/folder/file.zip?raw=true
如果您不介意克隆整个目录,这个小bash/zsh函数的最终结果是将单个文件克隆到当前目录中(通过将repo克隆到临时目录中,然后删除它)。
正方:你只能得到你想要的文件
缺点:你仍然需要等待整个回购复制
git-single-file () {
if [ $# -lt 2 ]
then
echo "Usage: $0 <repo url> <file path>"
return
fi
TEMP_DIR=$(mktemp -d)
git clone $1 $TEMP_DIR
cp $TEMP_DIR/$2 .
rm -rf $TEMP_DIR
}