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

举个GitHub repo的例子:

git@github.com:foobar/Test.git

其目录结构:

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

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


当前回答

一个简单的答案是从下面的链接中选择第一个乌龟svn。

https://tortoisesvn.net/downloads.html

安装时打开CLI选项,以便可以从命令行界面使用它。

复制github子目录链接。

实例https://github.com/tensorflow/models/tree/master/research/deeplab

用树干替换树/主

https://github.com/tensorflow/models/trunk/research/deeplab

并且做到了

svn检出https://github.com/tensorflow/models/trunk/research/deeplab

文件将被下载到当前目录中的deeplab文件夹中。

其他回答

一个简单的答案是从下面的链接中选择第一个乌龟svn。

https://tortoisesvn.net/downloads.html

安装时打开CLI选项,以便可以从命令行界面使用它。

复制github子目录链接。

实例https://github.com/tensorflow/models/tree/master/research/deeplab

用树干替换树/主

https://github.com/tensorflow/models/trunk/research/deeplab

并且做到了

svn检出https://github.com/tensorflow/models/trunk/research/deeplab

文件将被下载到当前目录中的deeplab文件夹中。

此功能有两个选项:

选项1:GitZip浏览器扩展

Chrome扩展、Edge扩展、Firefox插件

用法:

浏览任何Github存储库页面。两种下载方式:选择项目:默认情况下,您可以双击项目或选中项目前面的复选框。单击页面右下角的下载按钮。在上下文菜单中:单击“GitZip下载”>“整个存储库”或“当前文件夹”。将鼠标光标移动到项目上,然后单击“GitZip下载”>“选定文件夹/文件”。完成2-1-1后,单击“GitZip下载”>“已检查项目”。查看进度仪表板并等待浏览器触发器下载。获取ZIP文件。

获取令牌:

单击浏览器上的GitZip Extension图标。单击“获取令牌”之外的“普通”或“专用”链接。在Github身份验证页面上授权GitZip权限。返回到开始的回购页面。继续使用。


选项2:Github gh页面

http://kinolien.github.io/gitzip通过使用GitHub API和JSZip、FileSaver.js库。

步骤1:在右上角的字段中输入github url。步骤2:按回车键或直接单击下载以下载zip,或单击搜索以查看子文件夹和文件的列表。步骤3:单击“下载Zip文件”或“获取文件”按钮获取文件。

在大多数情况下,它工作正常,除了文件夹包含1000多个文件,因为Github Trees API的限制。(参考Github API#内容)

此外,如果您拥有GitHub帐户并在该站点中使用“获取令牌”链接,它还可以支持私有/公共回购并升级费率限制。

这是我用git v2.25.0做的,也是用v2.26.2测试的。这个技巧不适用于v2.30.1

TLDR

git clone --no-checkout --filter=tree:0 https://github.com/opencv/opencv
cd opencv

# requires git 2.25.x to 2.26.2
git sparse-checkout set data/haarcascades

您可以使用Docker来避免安装特定版本的git

git clone --no-checkout --filter=tree:0 https://github.com/opencv/opencv
cd opencv

# requires git 2.25.x to 2.26.2
docker run --rm -it -v $PWD/:/code/ --workdir=/code/ alpine/git:v2.26.2 sparse-checkout set data/haarcascades

完整解决方案

# bare minimum clone of opencv
$ git clone --no-checkout --filter=tree:0 https://github.com/opencv/opencv
...
Resolving deltas: 100% (529/529), done.

# Downloaded only ~7.3MB , takes ~3 seconds
# du = disk usage, -s = summary, -h = human-readable
$ du -sh opencv
7.3M    opencv/

# Set target dir
$ cd opencv
$ git sparse-checkout set data/haarcascades
...
Updating files: 100% (17/17), done.
# Takes ~10 seconds, depending on your specs

# View downloaded files
$ du -sh data/haarcascades/
9.4M    data/haarcascades/
$ ls data/haarcascades/
haarcascade_eye.xml                      haarcascade_frontalface_alt2.xml      haarcascade_licence_plate_rus_16stages.xml  haarcascade_smile.xml
haarcascade_eye_tree_eyeglasses.xml      haarcascade_frontalface_alt_tree.xml  haarcascade_lowerbody.xml                   haarcascade_upperbody.xml
haarcascade_frontalcatface.xml           haarcascade_frontalface_default.xml   haarcascade_profileface.xml
haarcascade_frontalcatface_extended.xml  haarcascade_fullbody.xml              haarcascade_righteye_2splits.xml
haarcascade_frontalface_alt.xml          haarcascade_lefteye_2splits.xml       haarcascade_russian_plate_number.xml

工具书类

git稀疏签出日志git稀疏签出文档gitfilter props文档

试试看。

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"

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

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,用于打开存储库并下载文件/文件夹。