git 在命令行中从 github 下载特定文件,而不是克隆整个 repo

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9159894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:54:50  来源:igfitidea点击:

Download specific files from github in command line, not clone the entire repo

gitgithub

提问by anjanesh

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

如何使用命令行仅从 github 下载 2 个文件?
类似于以下内容:

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

回答by dbrumann

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

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

$changeset_hash您也可以提供一个分支(例如 master)或标签来代替。

You can retrieve the raw file using something like wget.

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

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

由于数据的存储方式,直接从 .git-repository 访问单个文件是不可能的(据我所知)。

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 typesto get the raw data. This might look something like this:

编辑:当您想从私有存储库访问文件时,您首先必须在您的帐户设置中创建一个具有适当权限的访问令牌。您可以使用github 的 API 来访问文件的内容,而不是调用上面的 url 。请务必使用自定义媒体类型的 Accept-header来获取原始数据。这可能看起来像这样:

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

The -Owill 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.

-O会在本地保存文件的内容具有相同的名称作为远程文件名。为了更容易使用,您可以将其包装在脚本中。@Chris_Withers 建议使用一个不错的 python 片段进行编辑,但不幸的是,由于答案的重大变化而被拒绝。

回答by user1115547

Copy the specific file's raw link from GitHub.(As you open the file in Github, on the top right corner you can see the option to open the file in raw mode. Open it in raw mode and copy the URL)

从 GitHub 复制特定文件的原始链接。(在 Github 中打开文件时,在右上角可以看到以原始模式打开文件的选项。以原始模式打开并复制 URL)

Now use curl command in command line to download the file.

现在在命令行中使用 curl 命令下载文件。

curl -o filename raw-link-to-file

回答by vpatil

git checkout  

E.g:

例如:

git checkout master~2 file1

(git checkout --helpfor help)

git checkout --help寻求帮助)

回答by Gyumeijie

You can try github-files-fetcher, it is a command line tool which downloads a single folder or file from a GitHub repo.

您可以尝试github-files-fetcher,它是一个命令行工具,可从 GitHub 存储库下载单个文件夹或文件。

Given the example above, you can use the following command to fetch the two specific files from github:

鉴于上面的示例,您可以使用以下命令从 github 中获取两个特定文件:

fetcher --url="git://github.com/username/Project.git/file1" 
fetcher --url="git://github.com/username/Project.git/file2" 

Think a more real scenario: you were visiting the following webpage page and wanna download the asyncsubdirectory alone.

考虑一个更真实的场景:您正在访问以下网页页面并想async单独下载子目录。

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

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

sorry for not being allowed to post images.

很抱歉不允许发布图片。

With The github-files-fetcher, you should first copy the urlof that page, which is https://github.com/reduxjs/redux/tree/master/examples/async, and then run the command below in command line:

使用 The github-files-fetcher,您应该首先复制该url页面的https://github.com/reduxjs/redux/tree/master/examples/async,然后在命令行中运行以下命令:

fetcher --url=https://github.com/reduxjs/redux/tree/master/examples/async

fetcher --url=https://github.com/reduxjs/redux/tree/master/examples/async