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

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

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

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

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

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


当前回答

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

curl-H'授权:令牌INSERTACCESSTOKENHERE'-H'接受:application/vnd.github.v3.raw'-O-Lhttps://your_domain/api/v3/repos/owner/repo/contents/path

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

其他回答

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

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

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

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

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

例如,下载AFNetworking的README:

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

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

现在,这在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就是一个很好的例子。

您可以尝试github文件获取器,它是一个命令行工具,可以从github repo下载单个文件夹或文件。

想象一个真实的场景:您正在访问以下网页,并想单独下载异步子目录。

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

很抱歉不能发布图片。

使用github文件获取器,您应该首先复制该页面的urlhttps://github.com/reduxjs/redux/tree/master/examples/async,然后在命令行中运行以下命令:

提取器--url=https://github.com/reduxjs/redux/tree/master/examples/async

在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

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