从GitHub repo下载单个文件有哪些技巧?

我不想要显示原始文件的URL;对于二进制文件,什么都没有。

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

是否有可能将GitHub用作“下载服务器”?

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

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


当前回答

只需将wget与raw=True参数一起使用

wget "https://github.com/user/repository/blob/master/directory/file_name?raw=True" -O target_path/file_name

其他回答

在Mac或Linux上安装jq并使用它从Github中提取文件,如下所示:

curl -H 'Authorization: token <ACCESS_TOKEN>' \
   -H "Accept: application/vnd.github.v3+raw" -L \
   https://api.github.com/repos/MY-ORG/MY-REPO/contents/MY-FILE-PATH | \
   jq -r '.content' | base64 --decode > MY_FILE_NAME.txt

可以在此处找到上述命令的文档。

GitHub发布功能

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

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

我们很高兴宣布发布,这是一个向最终用户发送软件的工作流。发布是具有变更日志和二进制资产的一级对象,它们展示了Git工件之外的完整项目历史。发行版附带发行说明和下载软件或源代码的链接。按照许多Git项目的惯例,发布与Git标签绑定。您可以使用现有标记,或在发布标记时让发布者创建标记。

或者试试这个

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');

若您使用npm,只需运行以下命令行,最后,除了这个github文件,它不会下载任何npm包

npx config-pack https://github.com/facebook/react/blob/main/README.md

有关详细信息,请参阅配置包

可以这样使用curl:

卷曲-OLhttps://raw.githubusercontent.com/<username>/<repo名称>/<branch名称>/path/to/file

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