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

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

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

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

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

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


当前回答

要从Github repo下载文件,请使用带有原始文件链接的“curl”命令。

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

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

其他回答

或者试试这个

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

您应该只使用文件的原始URL进行操作。

例如,下载AFNetworking的README:

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

由于它是公共回购,因此您不需要任何凭据。请注意url的类型:raw.githubusercontent.com/path/to/file

2021,GitHub增加了一项新功能,即在web上打开visual studio代码。只需按句号或周期键即可启动。,当您在任何存储库中时。

因此,要下载任何特定文件,您可以按启动vscode。键,然后它将在vscode中显示存储库的所有文件。在那里,您可以通过右键单击>dowload下载所需的任何文件。

您链接到的页面回答了第一个问题。GitHub还提供了一个下载工具,用于发布等内容。谷歌代码根本没有Git。GitHub、Google Code和SourceForge是免费托管。SourceForge可能仍然会做CVS。

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

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

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