git 从 GitHub 下载单个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4604663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:45:17  来源:igfitidea点击:

Download single files from GitHub

gitversion-controlgithub

提问by Radek Simko

I guess most of you, developers, use any VCS, and I hope some of you use Git. Do you have any tip or trick how to get a download URL for a single file in a repository?

我想你们中的大多数人,开发人员,使用任何VCS,我希望你们中的一些人使用 Git。您是否有任何提示或技巧如何获取存储库中单个文件的下载 URL?

I don't want the URL for displaying the raw file; in case of binaries it's for nothing.

我不想要显示原始文件的 URL;在二进制文件的情况下,它毫无意义。

http://support.github.com/discussions/feature-requests/41-download-single-file

http://support.github.com/discussions/feature-requests/41-download-single-file

Is it even possible to use GitHub as a "download server"?

甚至可以将 GitHub 用作“下载服务器”吗?

If we decide to switch to Google Code, is the mentioned functionality presented here?

如果我们决定切换到 Google Code,这里是否提供了上述功能?

Or is there any free-of-charge hosting and VCS for open-source projects?

或者有没有免费的开源项目托管和VCS?

采纳答案by jonescb

Git does not support downloading parts of the repository. You have to download all of it. But you should be able to do this with GitHub.

Git 不支持下载部分存储库。您必须下载所有内容。但是你应该可以用 GitHub 做到这一点。

When you view a file it has a link to the "raw" version. The URLis constructed like so

当您查看文件时,它有一个指向“原始”版本的链接。该URL构造像这样

https://raw.githubusercontent.com/user/repository/branch/filename

By filling in the blanks in the URL, you can use Wgetor cURL(with the -Loption, see below) or whatever to download a single file. Again, you won't get any of the nice version control features used by Git by doing this.

通过填写 URL 中的空白,您可以使用WgetcURL(带有-L选项,见下文)或其他任何方式来下载单个文件。同样,您不会通过这样做获得 Git 使用的任何不错的版本控制功能。

Update: I noticed you mention this doesn't work for binary files. You probably shouldn't use binary files in your Git repository, but GitHub has a download section for each repository that you can use to upload files. If you need more than one binary, you can use a .zip file. The URL to download an uploaded file is:

更新:我注意到你提到这不适用于二进制文件。您可能不应该在您的 Git 存储库中使用二进制文件,但 GitHub 为每个存储库提供了一个下载部分,您可以使用它来上传文件。如果您需要多个二进制文件,您可以使用 .zip 文件。下载上传文件的 URL 是:

https://github.com/downloads/user/repository/filename

Note that the URLs given above, from the links on github.com, will redirect to raw.githubusercontent.com. You should not directly use the URL given by this HTTP 302 redirect because, per RFC 2616: "Since the redirection might be altered on occasion, the client SHOULD continue to use the Request-URI for future requests."

请注意,上面给出的 URL,从 上的链接github.com,将重定向到raw.githubusercontent.com。您不应直接使用此 HTTP 302 重定向给出的 URL,因为根据RFC 2616:“由于重定向有时可能会更改,因此客户端应该继续使用 Request-URI 来处理未来的请求。”

回答by bearfriend

  1. Go to the file you want to download.
  2. Click it to view the contents within the GitHub UI.
  3. In the top right, right clickthe Rawbutton.
  4. Save as...
  1. 转到您要下载的文件。
  2. 单击它以查看 GitHub UI 中的内容。
  3. 在右上角,右键单击Raw按钮。
  4. 另存为...

回答by thomasfuchs

You can use the V3 API to get a raw file like this (you'll need an OAuth token):

您可以使用 V3 API 来获取这样的原始文件(您需要一个 OAuth 令牌):

curl -H 'Authorization: token INSERTACCESSTOKENHERE' -H 'Accept: application/vnd.github.v3.raw' -O -L https://api.github.com/repos/owner/repo/contents/path

curl -H 'Authorization: token INSERTACCESSTOKENHERE' -H 'Accept: application/vnd.github.v3.raw' -O -L https://api.github.com/repos/owner/repo/contents/path

All of this has to go on one line. The -Ooption saves the file in the current directory. You can use -o filenameto specify a different filename.

所有这些都必须在一条线上进行。该-O选项将文件保存在当前目录中。您可以使用-o filename来指定不同的文件名。

To get the OAuth token follow the instructions here: https://help.github.com/articles/creating-an-access-token-for-command-line-use

要获取 OAuth 令牌,请按照此处的说明进行操作:https: //help.github.com/articles/creating-an-access-token-for-command-line-use

I've written this up as a gist as well: https://gist.github.com/madrobby/9476733

我也把它写成了一个要点:https: //gist.github.com/madrobby/9476733

回答by Ted Cox

According to this gist, you can use wget or cURL:

根据这个要点,您可以使用 wget 或 cURL:

  1. Click the file name in a GitHub repo.
  2. Click Raw to display the file contents.
  3. Copy the URL in your browser.
  4. In the command line, run either:

    • wget --no-check-certificate --content-disposition https://URL-from-step3/
    • curl -LJO https://URL-from-step3/

    One more: in RHEL7.6, run the following:

    • wget https://URL-from-step3/ --output-document=FILE_NAME
  1. 单击 GitHub 存储库中的文件名。
  2. 单击 Raw 以显示文件内容。
  3. 在浏览器中复制 URL。
  4. 在命令行中,运行:

    • wget --no-check-certificate --content-disposition https://URL-from-step3/
    • curl -LJO https://URL-from-step3/

    再一个:在 RHEL7.6 中,运行以下命令:

    • wget https://URL-from-step3/ --output-document=FILE_NAME

回答by Cam Song

GitHub Matemakes single file download effortless, just click the icon to download, currently it only work on Chrome.

GitHub Mate让单个文件下载变得轻而易举,只需点击图标即可下载,目前仅适用于 Chrome。

GitHub Mate Download

GitHub 伴侣下载

回答by Joe Leo

This is now possible in GitHub for any file. You need to translate your files for raw.github.com. For example, if your file is in your repository at:

这现在可以在 GitHub 中用于任何文件。您需要为 raw.github.com 翻译您的文件。例如,如果您的文件位于您的存储库中:

https://github.com/<username>/<repo>/some_directory/file.rb

Using wget you can grab the raw file from:

使用 wget 您可以从以下位置获取原始文件:

https://raw.github.com/<username>/<repo>/<branch>/some_directory/file.rb

Rails Composeris a great example of this.

Rails Composer就是一个很好的例子。

回答by Mark

You can use curlthis way:

你可以这样使用curl

curl -OL https://raw.githubusercontent.com/<username>/<repo-name>/<branch-name>/path/to/file

curl -OL https://raw.githubusercontent.com/<username>/<repo-name>/<branch-name>/path/to/file

Omeans that curl downloads the content
Lmeans that curl follows the redirection

O表示 curl 下载内容
L表示 curl 遵循重定向

回答by Abraham Murciano Benzadon

There is a chrome extension called Enhanced Github

有一个名为Enhanced Github的 chrome 扩展

It will add a download button directly to the right of each file.

它将直接在每个文件的右侧添加一个下载按钮。

enter image description here

在此处输入图片说明

回答by Drew Michel

To follow up with what thomasfuchs said but instead for GitHub Enterprise users here's what you can use.

要跟进 thomasfuchs 所说的内容,但对于 GitHub Enterprise 用户,您可以使用以下内容。

curl -H 'Authorization: token INSERTACCESSTOKENHERE' -H 'Accept: application/vnd.github.v3.raw' -O -L https://your_domain/api/v3/repos/owner/repo/contents/path

curl -H 'Authorization: token INSERTACCESSTOKENHERE' -H 'Accept: application/vnd.github.v3.raw' -O -L https://your_domain/api/v3/repos/owner/repo/contents/path

Also here's the API documentation https://developer.github.com/v3/repos/contents

还有这里的 API 文档https://developer.github.com/v3/repos/contents

回答by DJJ

In case you want to download a zip file from githubusing wget

如果您想从github使用下载 zip 文件wget

wget -O filename.zip https://github.com/downloads/user/repository/filename.zip?raw=true

see this websitefor more details

请参阅此网站了解更多详情