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

举个GitHub repo的例子:

git@github.com:foobar/Test.git

其目录结构:

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

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


当前回答

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

其他回答

此功能有两个选项:

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

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上测试。

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

试试看。

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 init
git remote add origin -f repoUrl // folder url
touch .git/info/sparse-checkout
git pull origin master

只有4行代码