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

举个GitHub repo的例子:

git@github.com:foobar/Test.git

其目录结构:

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

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


当前回答

我找到的最简单的方法是如何仅克隆Git存储库的子目录?

我在gitclone命令中添加了-b<branch>--singlebranch以下载特定的分支。所以我使用的命令是:

git clone --depth 1 --single-branch -b <branch> --filter=blob:none --sparse <url>
git sparse-checkout set <directory>

其他回答

转到DownGit>输入您的URL>下载!

您可以直接下载或从DownGit为任何GitHub公共目录或文件创建下载链接-



您还可以配置下载文件的财产-详细用法。


免责声明:我和提问者陷入了同样的问题,无法找到任何简单的解决方案。因此,我首先开发了这个工具供自己使用,然后为每个人打开它:)

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

要从GitHub导出目录,请将目录url中的“/tree/master/”替换为“trunk/”。

例如,要从以下URL导出目录:

https://github.com/liferay/liferay-plugins/tree/master/portlets/sample-hibernate-portlet

运行以下命令:

svn export https://github.com/liferay/liferay-plugins/trunk/portlets/sample-hibernate-portlet

无论出于什么原因,svn解决方案对我来说都不起作用,而且由于我不需要svn来做任何其他事情,所以花时间尝试它是没有意义的,所以我使用我已经拥有的工具来寻找一个简单的解决方案。该脚本只使用curl和awk来下载GitHub目录中的所有文件,该目录描述为“/:user:repo/contents/:path”。

GitHub REST API调用的返回主体“GET/repos/:user:repo/contents/:path”命令返回一个对象,该对象包含目录中每个文件的“download_url”链接。

该命令行脚本使用curl调用REST API,并通过AWK发送结果,AWK过滤掉除“download_url”行之外的所有行,删除链接中的引号和逗号,然后使用另一个对curl的调用下载链接。

curl -s https://api.github.com/repos/:user/:repo/contents/:path | awk \
     '/download_url/ { gsub("\"|,", "", $2); system("curl -O "$2"); }'

可以按以下方式使用git-svn。

首先,用主干替换树/主节点然后,通过sudoaptinstallgitsvn安装gitsvn

git svn clone https://github.com/lodash/lodash/trunk/test

这样,您就不必经历设置svn的痛苦,特别是针对Windows用户。