在命令行中从github下载特定文件,而不是克隆整个仓库 [英] Download specific files from github in command line, not clone the entire repo

查看:443
本文介绍了在命令行中从github下载特定文件,而不是克隆整个仓库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用命令行从github下载仅2个文件?
符合以下条件:

How do I download just 2 files from github using command line ?
Something in the lines of :

git fetch git://github.com/username/Project.git/file1
git fetch git://github.com/username/Project.git/file2

推荐答案

如果您转到页面并查看"raw"(在查看文件时位于左上角)提供的链接.您将看到,可以通过以下方式访问它:

If you go to the page and view the links provided by "raw" (in the top left corner, when viewing the file). You will see, that you can access it by:

https://github.com/username/repository/raw/$changeset_hash/path/to/file

除了$changeset_hash,您还可以提供分支(例如master)或标签.

Instead of $changeset_hash you can also provide a branch (e.g. master) or tag.

您可以使用wget之类的方法检索原始文件.

You can retrieve the raw file using something like wget.

(据我所知)无法直接从.git存储库访问单个文件,因为数据的存储方式.

Accessing a single file directly from a .git-repository is not possible (as far as I know), because of how the data is stored.

编辑:要从私有存储库访问文件时,首先必须在帐户设置中创建具有适当权限的访问令牌.除了调用上面的网址,您还可以使用 github的API来访问文件.确保对自定义媒体类型使用接受标头获取原始数据.这可能看起来像这样:

edit: When you want to access a file from a private repo, you first have to create an access token with the appropriate permissions in your account settings. Instead of calling the url above you can then use github's API to access the content of a file. Be sure to use the Accept-header for custom media types to get the raw data. This might look something like this:

curl \
  -H 'Authorization: token $YOUR_TOKEN' \
  -H 'Accept: application/vnd.github.v3.raw' \
  -O \
  -L 'https://api.github.com/repos/:owner/:repo/contents/:path'

-O将内容保存在与远程文件名相同名称的本地文件中.为了更容易使用,您可以将其包装在脚本中. @Chris_Withers建议使用一个不错的python代码段进行编辑,但不幸的是,答案的大部分更改都被拒绝了.

The -O will save the contents in a local file with the same name as the remote file name. For easier use you can wrap it in a script. @Chris_Withers suggested an edit with a nice python snippet that unfortunately got rejected as to big of a change to the answer.

这篇关于在命令行中从github下载特定文件,而不是克隆整个仓库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆