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

举个GitHub repo的例子:

git@github.com:foobar/Test.git

其目录结构:

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

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


当前回答

通过在url中将github替换为githubbox,打开repo到codesandbox,然后在codesandbox上转到文件菜单并将其导出为zip。

对于以下回购:https://github.com/geist-org/react/tree/master/examples/custom-themes

输入以下url:https://githubbox.com/geist-org/react/tree/master/examples/custom-themes

在codesandbox中,转到文件菜单并将其导出为Zip。

其他回答

你不能;不同于Subversion,每个子目录都可以单独检出,Git在整个存储库的基础上运行。

对于需要更细粒度访问的项目,可以使用子模块——每个子模块都是一个单独的Git项目,因此可以单独克隆。

可以想象,Git前端(例如GitHub的web界面或gitweb)可以选择为您提供一个界面来提取给定的文件夹,但据我所知,他们中没有一个这样做(尽管他们确实允许您下载单个文件,所以如果文件夹不包含太多文件,这是一个选项)

编辑-GitHub实际上提供了通过SVN的访问,这将允许您执行此操作(根据评论)。看见https://github.com/blog/1438-improved-svn-here-to-stay-old-svn-going-away有关如何执行此操作的最新说明

试试看。

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"

最简单的方法是使用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

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

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

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

在尝试了所有答案后,对我来说最好的解决方案是:

GitHub基于vscode的编辑器。

赞成的意见:

不需要任何额外的工具,如svn或API令牌。内容大小无限制另存为目录或文件,而不是存档。

说明书

转到任何回购。(例如。https://github.com/RespiraWorks/Ventilator/tree/master/software)按或者在URL中将.com替换为.dev,以在GitHub的内部编辑器中打开repo在资源管理器窗格(左侧或按Ctrl+Shift+E)中,右键单击所需的文件/文件夹并选择下载。在“选择文件夹”对话框中,选择磁盘上希望所选文件/文件夹存在的目录。

Note

我尝试了其他解决方案,如接受的答案,

不要只为此安装和学习svn。其他工具,如Download Directory、Refined GitHub、GitZip和DownGit,要么需要API令牌,要么无法下载大型目录。

其他选项

带有远程存储库扩展名的VSCode,用于打开存储库并下载文件/文件夹。