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

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

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

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

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

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


当前回答

转到脚本并单击“原始”

然后复制链接并使用aria2c链接下载。

例如:aria2chttps://raw.githubusercontent.com/kodamail/gscript/master/color.gsf

窍门:我想下载的文件是,https://github.com/kodamail/gscript*/blob*/master/color.gsf

只需将链接修改为https://raw.githubusercontent.com/kodamail/gscript/master/color.gsf

删除斜体文本并添加相同格式的粗体文本,这将为您提供正确的链接。

可以与aria2c、wget或curl一起使用,我在这里使用了aria2c。

其他回答

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

git文件

raw

I.如果您想下载.so之类的文件而不是源代码,请尝试单击下面的下载按钮:

该文件将从您的浏览器下载。

二、如果您想下载源代码,请单击“原始”,然后转到“原始”。。页

简单地复制/粘贴或使用curl或wget命令在终端中获取它。

对于使用GitHub Enterprise的用户,您需要按照以下方案构造URL

调用WebRequesthttp://github.mycompany.com/api/v3/repos/my-org/my-repo/contents/myfiles/file.txt-标头@{“Authorization”=“token 8d795936d2c1b2806587719b9b6456bd16549ad8”}

详细信息可在此处找到

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

或者试试这个

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

您可以使用V3API获取如下原始文件(需要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令牌,请遵循以下说明。

我也把这个写下来作为要点。