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

举个GitHub repo的例子:

git@github.com:foobar/Test.git

其目录结构:

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

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


当前回答

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

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

其他回答

gitclone--筛选器仅下载所需文件

例如,要仅克隆此存储库的子目录big/所需的对象:https://github.com/cirosantilli/test-git-partial-clone-big-small我可以做到:

git clone --depth 1 --filter=blob:none --sparse \
  https://github.com/cirosantilli/test-git-partial-clone-big-small
cd test-git-partial-clone-big-small
git sparse-checkout set small

--filter选项是与远程协议的更新一起添加的,它确实防止了从服务器下载对象。

我在下面的文章中详细介绍了这一点:如何仅克隆Git存储库的子目录?

2021 1月在git 2.30.0上测试。

您可以简单地下载目录树:

git archive --remote git@github.com:foobar/Test.git HEAD:foo | tar xf -

但如果你想检查一下,并且能够提交并将它们推回去,那么你就不能这样做。

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

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

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

免责声明:我做到了。

试试看。

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"

这是SVN优于Git的少数几个地方之一。

最终,我们倾向于三种选择:

使用wget从GitHub获取数据(使用原始文件视图)。让上游项目将所需的数据子集发布为构建工件。放弃并使用全额结账。它在第一个版本中大受欢迎,但除非您获得大量流量,否则在接下来的版本中不会太麻烦。