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

举个GitHub repo的例子:

git@github.com:foobar/Test.git

其目录结构:

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

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


当前回答

要从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

其他回答

要从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

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

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

为了放大上面的答案,一个从真正的GitHub存储库到本地目录的真实示例是:

svn ls https://github.com/rdcarp/playing-cards/trunk/PumpkinSoup.PlayingCards.Interfaces

svn export https://github.com/rdcarp/playing-cards/trunk/PumpkinSoup.PlayingCards.Interfaces  /temp/SvnExport/Washburn

有时,一个具体的例子有助于澄清所提出的替代方案。

你不能;不同于Subversion,每个子目录都可以单独检出,Git在整个存储库的基础上运行。

对于需要更细粒度访问的项目,可以使用子模块——每个子模块都是一个单独的Git项目,因此可以单独克隆。

可以想象,Git前端(例如GitHub的web界面或gitweb)可以选择为您提供一个界面来提取给定的文件夹,但据我所知,他们中没有一个这样做(尽管他们确实允许您下载单个文件,所以如果文件夹不包含太多文件,这是一个选项)

编辑-GitHub实际上提供了通过SVN的访问,这将允许您执行此操作(根据评论)。看见https://github.com/blog/1438-improved-svn-here-to-stay-old-svn-going-away有关如何执行此操作的最新说明

如果您有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