从存储库中检索单个文件
从远程 git 存储库获取单个文件的内容的最有效的机制(就传输的数据和使用的磁盘空间而言)是什么?
到目前为止,我已经设法想出:
git clone --no-checkout --depth 1 [email protected]:foo/bar.git && cd bar && git show HEAD:path/to/file.txt
这似乎仍然有点矫枉过正。
从存储库获取多个文件怎么样?
What is the most efficient mechanism (in respect to data transferred and disk space used) to get the contents of a single file from a remote git repository?
So far I've managed to come up with:
git clone --no-checkout --depth 1 [email protected]:foo/bar.git && cd bar && git show HEAD:path/to/file.txt
This still seems overkill.
What about getting multiple files from the repo?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(25)
在 git 版本 1.7.9.5 中,这似乎可以从远程导出单个文件,
这将捕获文件 README.md 的内容。
In git version 1.7.9.5 this seems to work to export a single file from a remote
This will cat the contents of the file
README.md
.继 Jakub 的 答案。
git archive
生成 tar 或 zip 存档,因此您需要通过 tar 通过管道传输输出以获取文件内容:将在当前目录中从远程存储库的 HEAD 中保存“filename”的副本。
:path/to/directory
部分是可选的。 如果排除,获取的文件将保存到<当前工作目录>/path/to/directory/filename
此外,如果您想启用
git archive --remote< /code> 在 git-daemon 托管的 Git 存储库上,您需要启用 daemon.uploadarch 配置选项。 请参阅 https://kernel.org/pub/software/scm/ git/docs/git-daemon.html
Following on from Jakub's answer.
git archive
produces a tar or zip archive, so you need to pipe the output through tar to get the file content:Will save a copy of 'filename' from the HEAD of the remote repository in the current directory.
The
:path/to/directory
part is optional. If excluded, the fetched file will be saved to<current working dir>/path/to/directory/filename
In addition, if you want to enable use of
git archive --remote
on Git repositories hosted by git-daemon, you need to enable the daemon.uploadarch config option. See https://kernel.org/pub/software/scm/git/docs/git-daemon.html如果部署了Web界面(如gitweb、cgit、Gitorious、ginatra),您可以使用它下载单个文件(“原始”或“普通”视图)。
如果对方启用了它,您可以使用git archive 的 '
--remote=
' 选项(并可能将其限制为给定文件所在的目录),例如:If there is web interface deployed (like gitweb, cgit, Gitorious, ginatra), you can use it to download single file ('raw' or 'plain' view).
If other side enabled it, you can use git archive's '
--remote=<URL>
' option (and possibly limit it to a directory given file resides in), for example:一般情况下不会,但如果您使用 Github:
对我来说
wget
到原始 url 是下载特定文件的最佳和最简单的方法。在浏览器中打开文件,然后单击“Raw”按钮。 现在刷新您的浏览器,复制 URL 并对其执行
wget
或curl
操作。wget 示例:
卷曲示例:
Not in general but if you are using Github:
For me
wget
to the raw url turned out to be the best and easiest way to download one particular file.Open the file in the browser and click on "Raw" button. Now refresh your browser, copy the url and do a
wget
orcurl
on it.wget example:
Curl example:
从远程导出单个文件:
这会将文件
README.md
下载到您的当前目录。如果您希望将文件内容导出到 STDOUT:
您可以在命令末尾提供多个路径。
To export a single file from a remote:
This will download the file
README.md
to your current directory.If you want the contents of the file exported to STDOUT:
You can provide multiple paths at the end of the command.
如果没有其他答案有效(即限制性 GitLab 访问),您可以通过以下方式执行“选择性签出”:
git clone --no-checkout --depth=1 --no-tags URL
git Restore --staged DIR-OR-FILE
git checkout DIR-OR-FILE
虽然此解决方案 100% git 兼容并且您可以签出目录,但它不是磁盘或网络最佳选择就像对文件执行 wget/curl 一样。
If no other answer worked (i.e. restrictive GitLab access), you can do a "selective-checkout" by:
git clone --no-checkout --depth=1 --no-tags URL
git restore --staged DIR-OR-FILE
git checkout DIR-OR-FILE
Although this solution is 100% git compliant and you can checkout a directory, it's not disk nor network optimal as doing a wget/curl on a file.
我用这种方式解决了:
如果你愿意,你可以将“BranchName”替换为“HEAD”
I solved in this way:
If you want, you could replace "BranchName" for "HEAD"
这对我来说似乎是一个解决方案: http://gitready.com/intermediate/2009/02/27/get-a-file-from-a-specific-revision.html
其中
4
表示四个版本now 和~
是评论中提到的波浪号。It looks like a solution to me: http://gitready.com/intermediate/2009/02/27/get-a-file-from-a-specific-revision.html
where
4
means four revision from now and~
is a tilde as mentioned in the comment.这里的一些答案的微妙变体回答了OP的问题:
A nuanced variant of some of the answers here that answers the OP's question:
我用这个
I use this
这是特定于 GitHub 上托管的 git 存储库的。
请尝试使用 Github 命令行应用程序
gh
的 'api' 命令来进行经过身份验证的调用Github 的“获取存储库内容”端点。基本命令是:
作为一个额外的好处,当您从包含您尝试从中获取文件的存储库的克隆的目录中执行此操作时,{owner} 和 {repo} 部分将自动填充。
https://docs.github.com/en/rest/reference /repos#get-repository-content
响应将是一个 JSON 对象。 如果是 确实指向一个文件,JSON 将包含“大小”、“名称”、多个用于访问该文件的 url 字段,以及一个“内容”字段,它是文件内容的 Base64 编码版本。
要获取文件内容,您可以卷曲“download_url”的值,或者仅解码“content”字段。 您可以通过管道传递 base64 命令来做到这一点,如下所示:
This is specific for git repos hosted on GitHub
Try the 'api' command of Github's command line app,
gh
, to make an authenticated call to Github's 'get repository contents' endpoint.The basic command is:
As an added bonus, when you do this from inside a directory that contains a clone of the repo you're trying to get the file from, the {owner} and {repo} part will be automatically filled in.
https://docs.github.com/en/rest/reference/repos#get-repository-content
The response will be a JSON object. If the <path_to_the_file> indeed points to a file, the JSON will include a 'size', 'name', several url fields to access the file, as well as a 'content' field, which is a base64 encoded version of the file contents.
To get the file contents, you can curl the value of the "download_url", or just decode the 'content' field. You can do that by piping the base64 command, like this:
在我看来,使用以下内容是最简单的方法:
It seems to me the easiest way to use the following:
如果您的存储库支持令牌(例如 GitLab),则为您的用户生成一个令牌,然后导航到您要下载的文件并单击 RAW 输出以获取 URL。 要下载文件,请使用:
If you repository supports tokens (for example GitLab) then generate a token for your user then navigate to the file you will download and click on RAW output to get the URL. To download the file use:
对于单个文件,只需使用 wget 命令。
首先,按照下图点击“raw”获取url,否则您将下载嵌入html的代码。
然后,浏览器将打开一个新页面,其 url 以 https://raw 开头。 githubusercontent.com/...
只需在终端中输入命令:
一会儿文件就会放入您的文件夹中。
For single file, just use wget command.
First, follow the pic below to click "raw" to get the url, otherwise you will download code embedded in html.
Then, the browser will open a new page with url start with https://raw.githubusercontent.com/...
just enter the command in the terminal:
A while the file will put in your folder.
如果您的 Git 存储库托管在 Azure-DevOps (VSTS) 上,您可以使用 Rest API。
该 API 的格式如下所示:
例如:
If your Git repository hosted on Azure-DevOps (VSTS) you can retrieve a single file with Rest API.
The format of this API looks like this:
For example:
以下 2 个命令对我有用:
git archive --remote={remote_repo_git_url} {branch} {file_to_download} -o {tar_out_file}
下载
file_to_download
作为tar< /code> 来自远程存储库的
branch
的存档,其 url 为remote_repo_git_url
并将其存储在tar_out_file
tar -x -f {tar_out_file} .tar
从tar_out_file
中提取file_to_download
The following 2 commands worked for me:
git archive --remote={remote_repo_git_url} {branch} {file_to_download} -o {tar_out_file}
Downloads
file_to_download
astar
archive frombranch
of remote repository whose url isremote_repo_git_url
and stores it intar_out_file
tar -x -f {tar_out_file}.tar
extracts thefile_to_download
fromtar_out_file
我使用curl,它适用于公共存储库或通过Web 界面使用https 基本身份验证的存储库。
curl -L --retry 20 --retry-delay 2 -O https://github.com/ACCOUNT/REPO/raw/master/PATH/TO/FILE/FILE.TXT -u 用户:密码
我已经在 github 和 bitbucket 上测试过它,两者都适用。
I use curl, it works with public repos or those using https basic authentication via a web interface.
curl -L --retry 20 --retry-delay 2 -O https://github.com/ACCOUNT/REPO/raw/master/PATH/TO/FILE/FILE.TXT -u USER:PASSWORD
I've tested it on github and bitbucket, works on both.
Yisrael Dov 的答案很简单,但它不允许压缩。 您可以使用
--format=zip
,但您无法像使用 tar 一样使用管道命令直接解压缩它,因此您需要将其保存为临时文件。 这是一个脚本:这也适用于目录。
Yisrael Dov's answer is the straightforward one, but it doesn't allow compression. You can use
--format=zip
, but you can't directly unzip that with a pipe command like you can with tar, so you need to save it as a temporary file. Here's a script:This works with directories too.
Github 企业解决方案
Github Enterprise Solution
如果你想从特定的哈希+远程存储库获取文件,我尝试过 git-archive 但它不起作用。
您必须使用 git clone,一旦克隆存储库,您就必须使用 git-archive 才能使其工作。
我在 git 存档中发布了一个关于如何更简单地进行操作的问题来自远程的特定哈希
If you want to get a file from a specific hash + a remote repository I've tried git-archive and it didn't work.
You would have to use git clone and once the repository is cloned you would have then to use git-archive to make it work.
I post a question about how to do it more simpler in git archive from a specific hash from remote
对于直接从浏览器(我使用 safari...)的 bitbucket,右键单击“查看原始数据”并选择“下载链接文件”:
for bitbucket directly from browser (I used safari...) right-click on 'View Raw" and choose "Download Linked File":
如果您不介意克隆整个目录,这个小 bash/zsh 函数将获得将单个文件克隆到当前目录的最终结果(通过将存储库克隆到临时目录并随后将其删除)。
优点:你只能得到你想要的文件
缺点:你仍然需要等待整个存储库克隆
If you don't mind cloning the entire directory, this small bash/zsh function will have the end result of cloning a single file into your current directory (by cloning the repo into a temp directory and removing it afterwards).
Pro: You only get the file you want
Con: You still have to wait for the whole repo to clone
如果您的目标只是下载文件,那么有一个名为
gget
的无忧应用程序:上面的示例将从
hugo
存储库下载单个文件。https://github.com/dpb587/gget
If your goal is just to download the file there's a hassle-free application called
gget
:The above example would download single file from
hugo
repository.https://github.com/dpb587/gget
我创建了一个到目前为止对我有用的小 bash 脚本:
Simply ./(scriptName.sh) (原始 github 链接)(输出文件名)
我只是用它来获取 Pi Pico 的链接文件,它运行良好。
这里的关键是不要使用原始链接作为来源,而是解析
原始链接并创建原始文件的链接。 我不确定这样做是否有任何负面影响,所以如果有人比我对原始 github 内容有更多的见解,请随时添加。 作为旁注,请确保使用完整链接(即 https://),否则剪辑将会被关闭。
I created a little bash script that has worked for me so far:
Simply ./(scriptName.sh) (original github link) (output file name)
I just used it to grab the linkfile for a Pi Pico and it worked well.
The key here is to not use the original link as the source, rather parse the
original link and create a link to the raw file. I'm unsure if there's any negative side effects to doing it this way, so if anyone has any more insight than I do on raw github content feel free to add. As a side note, make sure to use the full link (i.e https://) otherwise the cuts will be off.
与@Steven Penny的答案相关,我也使用wget。 此外,为了决定将输出发送到哪个文件,我使用 -O 。
如果您使用 gitlabs,该 url 的另一种可能性是:
除非您有证书或从受信任的服务器访问 gitlabs 安装,否则您需要 --no-check-certificate 正如 @Kos 所说。 我更喜欢这样做而不是修改 .wgetrc ,但这取决于您的需要。
如果它是一个大文件,您可以考虑使用 wget 的 -c 选项。 如果之前的意图在中间失败,则能够从您离开的位置继续下载文件。
Related to @Steven Penny's answer, I also use wget. Furthermore, to decide which file to send the output to I use -O .
If you are using gitlabs another possibility for the url is:
Unless you have the certificate or you access from a trusted server for the gitlabs installation you need --no-check-certificate as @Kos said. I prefer that rather than modifying .wgetrc but it depends on your needs.
If it is a big file you might consider using -c option with wget. To be able to continue downloading the file from where you left it if the previous intent failed in the middle.