如何从GitHub上托管的远程Git repo中仅下载特定文件夹或目录?

举个GitHub repo的例子:

git@github.com:foobar/Test.git

其目录结构:

Test/
├── foo/ 
│   ├── a.py
│   └── b.py   
└── bar/
    ├── c.py
    └── d.py

我只想下载foo文件夹,而不是克隆整个测试项目。


当前回答

试试看。

https://github.com/twfb/git-directory-download

usage: gitd [-h] [-u URL] [-r] [-p] [--proxy PROXY]

optional arguments:
  -h, --help         show this help message and exit
  -u URL, --url URL  github url, split by ",", example: "https://x, http://y"
  -r, --raw          download from raw url
  -p, --parse        download by parsing html
  --proxy PROXY      proxy config, example "socks5://127.0.0.1:7891"

Example:
  1. download by raw url: gitd -u "https://github.com/twfb/git-directory-download"
  2. download by raw url: gitd -r -u "https://github.com/twfb/git-directory-download"
  3. dowmload by parsing: gitd -p -u "https://github.com/twfb/git-directory-download"
  4. download by raw url with proxy: gitd -r -u "https://github.com/twfb/git-directory-download" --proxy "socks5://127.0.0.1:7891"

其他回答

2019年总结

有多种方法来处理这一点,这取决于您是否希望手动或以编程方式执行此操作。

下面总结了四个选项。对于那些喜欢更实际的解释的人,我制作了一个YouTube视频:从GitHub下载个人文件和文件夹。

此外,对于那些需要从GitHub下载单个文件(而不是文件夹)的人,我在StackOverflow上发布了类似的答案。


1.GitHub用户界面

存储库主页上有一个下载按钮。当然,这将下载整个repo,之后您需要解压缩下载,然后手动拖出所需的特定文件夹。

2.第三方工具

有多种浏览器扩展和web应用可以处理这一问题,DownGit就是其中之一。只需将GitHub URL粘贴到文件夹(例如。https://github.com/babel/babel-eslint/tree/master/lib)然后按下“下载”按钮。

3.子版本

GitHub不支持git存档(允许我们下载特定文件夹的git功能)。然而,GitHub支持多种Subversion功能,我们可以使用其中一种功能。Subversion是一种版本控制系统(git的替代品)。您需要安装Subversion。获取要下载的文件夹的GitHub URL。不过,您需要修改此URL。您需要指向存储库的链接,后跟单词“trunk”,并以指向嵌套文件夹的路径结尾。换句话说,使用与我前面提到的相同的文件夹链接示例,我们将用“trunk”替换“tree/master”。最后,打开一个终端,导航到要下载内容的目录,输入以下命令(用您构建的URL替换URL):svn exporthttps://github.com/babel/babel-eslint/trunk/lib,然后按enter键。

4.GitHub API

如果您想以编程方式完成此任务,这是您需要的解决方案。这实际上是DownGit在幕后使用的。使用GitHub的REST API,编写一个脚本,向内容端点发出GET请求。端点的构造如下:https://api.github.com/repos/:owner/:repo/contents/:path.替换占位符后,示例端点为:https://api.github.com/repos/babel/babel-eslint/contents/lib.这将为该文件夹中存在的所有内容提供JSON数据。数据包含您所需的一切,包括内容是否是文件夹或文件,如果是文件,则包含下载URL,如果是文件夹,则包含API端点(以便您可以获取该文件夹的数据)。使用这些数据,脚本可以递归地遍历目标文件夹中的所有内容,为嵌套文件夹创建文件夹,并下载每个文件夹的所有文件。查看DownGit的代码以获得灵感。

试试看。

https://github.com/twfb/git-directory-download

usage: gitd [-h] [-u URL] [-r] [-p] [--proxy PROXY]

optional arguments:
  -h, --help         show this help message and exit
  -u URL, --url URL  github url, split by ",", example: "https://x, http://y"
  -r, --raw          download from raw url
  -p, --parse        download by parsing html
  --proxy PROXY      proxy config, example "socks5://127.0.0.1:7891"

Example:
  1. download by raw url: gitd -u "https://github.com/twfb/git-directory-download"
  2. download by raw url: gitd -r -u "https://github.com/twfb/git-directory-download"
  3. dowmload by parsing: gitd -p -u "https://github.com/twfb/git-directory-download"
  4. download by raw url with proxy: gitd -r -u "https://github.com/twfb/git-directory-download" --proxy "socks5://127.0.0.1:7891"

使用此函数,第一个参数是文件夹的url,第二个参数是下载文件夹的位置:

function github-dir() {
    svn export "$(sed 's/tree\/master/trunk/' <<< "$1")" "$2"  
}

如果您有svn,可以使用svn导出来执行以下操作:

svn export https://github.com/foobar/Test.git/trunk/foo

请注意URL格式:

基本URL为https://github.com//末尾附加的树干

在运行svn导出之前,最好先使用以下命令验证目录的内容:

svn ls https://github.com/foobar/Test.git/trunk/foo

您可以将ghget与从地址栏复制的任何URL一起使用:

ghget https://github.com/fivethirtyeight/data/tree/master/airline-safety

这是一个独立的可移植shell脚本,不使用SVN(这对我来说在大型回购中不起作用)。它也不使用API,因此不需要令牌,也不受速率限制。

免责声明:我做到了。