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

举个GitHub repo的例子:

git@github.com:foobar/Test.git

其目录结构:

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

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


当前回答

为了独特,我必须说,你也可以在没有svn、git或任何api的情况下下载Github文件夹。Github支持RAW链接,您可以利用该链接仅下载所需的文件和文件夹。

我注意到很多事情。以下是我的研究集:

机械装置

从网页中抓取所有超链接<a>并获取其href=“value”值如果href值包含“/tree/master/”或“/tree/main/”,则它是文件夹链接:https://github.com/graysuit/GithubFolderDownloader/tree/main/GithubFolderDownloader否则,如果href值包含“/bblo/master/”或“/bblo/main/”,则为文件链接:https://github.com/graysuit/GithubFolderDownloader/blob/main/GithubFolderDownloader.sln然后,将“github.com”替换为“raw.githubusercontent.com”,并从文件中删除“/blob/”:https://raw.githubusercontent.com/graysuit/GithubFolderDownloader/main/GithubFolderDownloader.sln它将成为RAW链接。现在你可以下载了。

Tool

在上述研究的基础上,我用C#创建了一个可以抓取文件夹的极简工具。graysuit/GithubFolderDownloader

注:我是作者。如果有任何遗漏或不清楚的地方,您可以发表评论。

其他回答

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

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

一个简单的答案是从下面的链接中选择第一个乌龟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文件夹中。

试试看。

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"

如果要下载的目录是一个单独的库,最好创建其他git repo,然后使用git子模块函数。

当然,你必须是你想要的初始回购的所有者

此功能有两个选项:

选项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帐户并在该站点中使用“获取令牌”链接,它还可以支持私有/公共回购并升级费率限制。