首页 文章

从GitHub下载单个文件

提问于
浏览
451

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

我不想要显示原始文件的URL;在二进制文件的情况下它是无用的 .

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

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

如果我们决定切换到Google代码,那么此处提供的功能是什么?

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

26 回答

  • 456

    Git不支持下载存储库的部分内容 . 你必须下载所有这些 . 但你应该能够用GitHub做到这一点 .

    查看文件时,它具有指向"raw"版本的链接 . URL就是这样构造的

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

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

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

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

    请注意,上面给出的URL,来自 github.com 上的链接,将重定向到 raw.githubusercontent.com . 您不应该直接使用此HTTP 302重定向提供的URL,因为,根据RFC 2616:"Since the redirection might be altered on occasion, the client SHOULD continue to use the Request-URI for future requests."

  • 1
    • 转到要下载的文件 .

    • 单击它以查看GitHub UI中的内容 .

    • 在右上角, right click Raw 按钮 .

    • 另存为...

  • 7

    您可以使用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

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

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

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

  • 13

    GitHub Mate使单个文件下载毫不费力,只需单击要下载的图标,目前它只适用于Chrome .

    GitHub Mate Download

  • 1

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

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

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

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

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

  • 2

    根据this gist,您可以使用wget或cURL:

    • 单击GitHub仓库中的文件名 .

    • 单击“原始”以显示文件内容 .

    • 在浏览器中复制URL .

    • 在命令行中,运行以下任一项:

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

    • curl -LJO https://URL-from-step3/

  • 431

    跟进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

    另外这里是API文档https://developer.github.com/v3/repos/contents

  • 11

    如果你想使用 wgetgithub 下载一个zip文件

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

    有关详细信息,请参阅website

  • 9

    您可以这样使用 curl

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

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

  • 0

    这种方法适用于Windows,因为我从未使用过MAC,因此我不知道MAC中的备用键是什么,我将在下面提到 .

    我们来谈谈CSV文件 . 如果要下载CSV文件:

    • 转到要下载的特定数据集,然后单击它 .

    • 您将在数据集的右上角看到"Raw"按钮 .

    • 按"Alt",然后左键单击"Raw"按钮 .

    • 整个CSV将在您的系统中下载 .

    记住,你必须按Alt并同时左键单击 . 只需单击“原始”按钮,即可在浏览器中打开CSV .

    我希望有所帮助 .

  • -2

    您应该使用文件的 raw URL来执行此操作 .

    例如,下载AFNetworking的自述文件:

    curl https://raw.githubusercontent.com/AFNetworking/AFNetworking/master/README.md > ADREADME.md
    

    由于它是一个公共回购,您不需要任何凭据 . 请注意网址的种类: raw.githubusercontent.com/path/to/file

  • 0
    • 您链接的页面回答了第一个问题 .

    • GitHub还有一个下载工具,用于发布版本 .

    • Google Code根本没有Git .

    • GitHub,谷歌代码和SourceForge,刚刚开始,是免费托管 . SourceForge可能会仍然CVS .

  • 1

    这肯定会奏效 . 至少在Chrome中 . 右键单击"Raw"图标 - >将链接另存为 .

  • 7

    我最近发现了一个名为 gitzip 的服务,它也是开源的:

    网站 - http://kinolien.github.io/gitzip/

    回购 - https://github.com/KinoLien/gitzip

    访问上述站点,输入repo或目录URL,您可以将单个文件或整个目录下载为zip文件 .

  • 3

    我认为新的 url 结构是 raw.giturl 例如:

    git file

    raw

  • 28

    如果您碰巧使用curl和firefox ......您可以使用cliget附加组件生成卷曲调用,包括所有身份验证机制(也称为cookie) .

    所以右键单击 raw 按钮cliget-> "copy url for link"然后将其粘贴到shell中 . 即使您必须登录才能看到它,您将获得您的文件 .

  • 4

    您应该使用GitHub的 Releases 功能将可下载数据(例如已编译的二进制文件)与用于生成该数据的源代码的标记版本相关联,而不是链接以下载回购中的特定文件 .

    https://github.com/blog/1547-release-your-software

    我们很高兴地宣布Releases,一个向最终用户发送软件的工作流程 . 发布是具有更改日志和二进制资产的一流对象,它们提供了超出Git工件的完整项目历史记录 . 发行版附带发行说明和下载软件或源代码的链接 . 遵循许多Git项目的约定,版本与Git标签相关联 . 您可以使用现有标记,也可以在发布标记时创建标记 .

    enter image description here

  • 1

    要从Github存储库下载文件,请使用'curl'命令以及指向原始文件的链接 .

    curl https://raw.githubusercontent.com/user/repo/filename --output filename
    

    添加--output选项,后跟新文件名,将原始文件下载到新创建的文件中 .

  • 2

    有一个名为Enhanced Github的镀铬扩展名

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

    enter image description here

  • 0
    • 在"Clone in Desktop"正下方的右侧,它说是"Download Zip file"

    • 下载Zip文件

    • 解压缩文件

  • 1

    对于使用GitHub Enterprise的用户,您需要在以下方案中构建URL

    Invoke-WebRequest http://github.mycompany.com/api/v3/repos/my-org/my-repo/contents/myfiles/file.txt -Headers @{"Authorization"="token 8d795936d2c1b2806587719b9b6456bd16549ad8"}

    详细信息可以在这里找到

    http://artisticcheese.blogspot.com/2017/04/how-to-download-individual-files-from.html

  • 2

    现在可以使用此Google Chrome扩展程序下载存储库中的任何文件或任何特定文件夹:

    GitZip for github : link : https://chrome.google.com/webstore/detail/gitzip-for-github/ffabmkklhbepgcgfonabamgnfafbdlkn

    用法:

    • 在任何GitHub公共转发页面中 .

    • 只需双击您需要的项目 .

    • 点击右下方的下载按钮 .

    • 查看进度仪表板并等待浏览器触发器下载 .

    • 获取ZIP文件 .

    enter image description here

    enter image description here

  • 0

    或试试这个

    const https = require('https');
    const fs = require('fs');
    const DOMAIN = 'raw.githubusercontent.com';
    
    function writeFile(data, fileName) {
      fs.appendFile(fileName, data.toString(), err => {
        if (err) {
          console.log('error in writing file', err);
        }
      });
    }
    
    function EOF(data) {
      console.log('EOF');
    }
    
    function getFileName(pathToFile) {
      var result = pathToFile.split('/');
      var splitLength = result.length;
      return result[splitLength - 1];
    }
    function getFile(branchName, username, repoName, ...pathToFile) {
      pathToFile.forEach(item => {
        const path = `/${username}/${repoName}/${branchName}/${item}`;
        const URL = `${DOMAIN}${path}`;
        const options = {
          hostname: DOMAIN,
          path: path
        };
        var fileName = getFileName(item);
    
        https
          .get(options, function(res) {
            console.log(res.statusCode);
            /* if file not found */
            if (res.statusCode === 404) {
              console.log('FILE NOT FOUND');
            } else {
              /* if file found */
              res.on('data', data => writeFile(data, fileName));
              res.on('end', data => EOF(data));
            }
          })
          .on('error', function(res) {
            console.log('error in reading URL');
          });
      });
    }
    getFile('master', 'bansalAyush', 'InstagramClone', '.babelrc', 'README.md');
    
  • 11

    我使用以下格式,我觉得告知路径非常重要 .

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

    ^^^以上在我的脑海里并不是很完整

    https://github.com/<user>/<repoROOTname>/blob/master/<path>/<filename>?raw=true
    

    有人说raw.github.com或raw而不是blob,但第二行对我有用,我希望能帮助别人......

  • 0

    您可以尝试github-files-fetcher,这是一个命令行工具,可以从GitHub仓库下载单个文件夹或文件 .

    想一个真实的场景:您正在访问以下网页页面并想单独下载 async 子目录 .

    https://github.com/reduxjs/redux/tree/master/examples

    抱歉不允许发布图片 .

    使用 github-files-fetcher ,您应该首先复制该页面的 url ,即https://github.com/reduxjs/redux/tree/master/examples/async,然后在命令行中运行以下命令:

    fetcher --url=https://github.com/reduxjs/redux/tree/master/examples/async

  • 0

    这对我来说现在很有用......

    • 在单独的选项卡中打开原始文件 .

    • 将记事本中的所有内容复制到新文件中 .

    • 将文件保存在原来的扩展名中

    ...刚刚为我的DL工作.php文件 .

相关问题