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

举个GitHub repo的例子:

git@github.com:foobar/Test.git

其目录结构:

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

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


当前回答

最简单的方法是使用fetcher

首先,使用以下命令安装fetcher:

npm安装-ggithub文件获取器

然后您可以下载带有URL的文件或文件夹:

fetcher--url=resource_url--out=output_directory

例如:

fetcher--url=“https://github.com/Gyumeijie/github-files-fetcher/blob/master/CHANGELOG.md“--out=/tmp

其他回答

如果您需要以编程方式执行,并且不想依赖SVN,则可以使用GitHubAPI递归下载所有内容。

为了获得灵感,以下是我的红宝石要点:https://gist.github.com/cvengros/b2a7e82f66519d423b6f

如果您想使用Python和SVN下载特定的GitHub目录,请使用以下代码:

import validators
from svn.remote import RemoteClient

def download_folder(url):
    if 'tree/master' in url:
        url = url.replace('tree/master', 'trunk')

    r = RemoteClient(url)
    r.export('output')


if __name__ == '__main__':
    url = input('Enter folder URL: ')
    if not validators.url(url):
        print('Invalid url')
    else:
        download_folder(url)

您可以在本教程中查看有关此代码和其他GitHub搜索和下载提示的更多详细信息:https://python.gotrained.com/search-github-api/

你可以尝试为chrome使用gitzip扩展,它超级容易使用,而且以前也帮我下载了很多文件夹!

如果在特殊情况下需要存储库中的某个文件,则会出现此问题。

这里可以找到一个简短的答案。您应该将url更改为以下格式:

https://raw.github.com/user/repository/branch/file.name

简单地解释一下,从github输入您想要的url。生吃。在url地址中的github之前,并从地址中删除blob。例如,假设您想要获取此地址中的csv文件:

https://github.com/CSSEGISandData/COVID-19/blob/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv

您应该将url更改为以下url:

https://raw.github.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv

试试看。

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"