从远程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
这似乎还是有些过分。
从回购中获取多个文件呢?
当前回答
如果你的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
如果有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 -
一般来说不是,但如果你使用Github:
对我来说,wget到原始url是下载一个特定文件的最佳和最简单的方法。
在浏览器中打开文件,点击“Raw”按钮。现在刷新浏览器,复制url并对其执行wget或curl操作。
wget的例子:
wget 'https://github.abc.abc.com/raw/abc/folder1/master/folder2/myfile.py?token=DDDDnkl92Kw8829jhXXoxBaVJIYW-h7zks5Vy9I-wA%3D%3D' -O myfile.py
旋度的例子:
curl 'https://example.com/raw.txt' > savedFile.txt
我是这样解决的:
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"
使用实例远程导出单个文件。
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
可以在命令末尾提供多个路径。