如何从GitHub上托管的远程Git repo中仅下载特定文件夹或目录?
举个GitHub repo的例子:
git@github.com:foobar/Test.git
其目录结构:
Test/
├── foo/
│ ├── a.py
│ └── b.py
└── bar/
├── c.py
└── d.py
我只想下载foo文件夹,而不是克隆整个测试项目。
如何从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文件夹中。
其他回答
另一个具体示例:
就像我想从url下载“iOS Pro Geo”文件夹
https://github.com/alokc83/APRESS-Books-Source-Code-/tree/master/%20Pro%20iOS%20Geo
我可以通过
svn checkout https://github.com/alokc83/APRESS-Books-Source-Code-/trunk/%20Pro%20iOS%20Geo
注意路径中的主干
编辑:(根据Tommie C的评论)
是的,使用导出而不是签出将提供一个干净的副本,而无需额外的git存储库文件。
svn export https://github.com/alokc83/APRESS-Books-Source-Code-/trunk/%20Pro%20iOS%20Geo
已编辑:如果树/主节点不在url中,则分叉它,它将在分叉的url中。
如果您有svn,可以使用svn导出来执行以下操作:
svn export https://github.com/foobar/Test.git/trunk/foo
请注意URL格式:
基本URL为https://github.com//末尾附加的树干
在运行svn导出之前,最好先使用以下命令验证目录的内容:
svn ls https://github.com/foobar/Test.git/trunk/foo
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上测试。
2019年总结
有多种方法来处理这一点,这取决于您是否希望手动或以编程方式执行此操作。
下面总结了四个选项。对于那些喜欢更实际的解释的人,我制作了一个YouTube视频:从GitHub下载个人文件和文件夹。
此外,对于那些需要从GitHub下载单个文件(而不是文件夹)的人,我在StackOverflow上发布了类似的答案。
1.GitHub用户界面
存储库主页上有一个下载按钮。当然,这将下载整个repo,之后您需要解压缩下载,然后手动拖出所需的特定文件夹。
2.第三方工具
有多种浏览器扩展和web应用可以处理这一问题,DownGit就是其中之一。只需将GitHub URL粘贴到文件夹(例如。https://github.com/babel/babel-eslint/tree/master/lib)然后按下“下载”按钮。
3.子版本
GitHub不支持git存档(允许我们下载特定文件夹的git功能)。然而,GitHub支持多种Subversion功能,我们可以使用其中一种功能。Subversion是一种版本控制系统(git的替代品)。您需要安装Subversion。获取要下载的文件夹的GitHub URL。不过,您需要修改此URL。您需要指向存储库的链接,后跟单词“trunk”,并以指向嵌套文件夹的路径结尾。换句话说,使用与我前面提到的相同的文件夹链接示例,我们将用“trunk”替换“tree/master”。最后,打开一个终端,导航到要下载内容的目录,输入以下命令(用您构建的URL替换URL):svn exporthttps://github.com/babel/babel-eslint/trunk/lib,然后按enter键。
4.GitHub API
如果您想以编程方式完成此任务,这是您需要的解决方案。这实际上是DownGit在幕后使用的。使用GitHub的REST API,编写一个脚本,向内容端点发出GET请求。端点的构造如下:https://api.github.com/repos/:owner/:repo/contents/:path.替换占位符后,示例端点为:https://api.github.com/repos/babel/babel-eslint/contents/lib.这将为该文件夹中存在的所有内容提供JSON数据。数据包含您所需的一切,包括内容是否是文件夹或文件,如果是文件,则包含下载URL,如果是文件夹,则包含API端点(以便您可以获取该文件夹的数据)。使用这些数据,脚本可以递归地遍历目标文件夹中的所有内容,为嵌套文件夹创建文件夹,并下载每个文件夹的所有文件。查看DownGit的代码以获得灵感。
这是我用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文档