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

举个GitHub repo的例子:

git@github.com:foobar/Test.git

其目录结构:

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

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


当前回答

对于通用git Repo:

如果您想下载文件,而不是克隆具有历史记录的存储库,可以使用gitarchive来完成此操作。

gitarchive生成git存储库的压缩zip或tar存档。让它与众不同的一些东西:

您可以选择git存储库中要归档的文件或目录。它不会将.git/文件夹或其运行的存储库中任何未跟踪的文件归档。您可以归档特定的分支、标记或提交。使用git管理的项目通常使用它来生成项目版本(beta、release、2.0等)的存档,供用户下载。

使用ssh从远程存储库创建docs/usage目录归档的示例:

# in terminal
$ git archive --format tar --remote ssh://server.org/path/to/git HEAD docs/usage > /tmp/usage_docs.tar

更多信息请参阅本博客文章和git文档。

GitHub Repos注意事项:

GitHub不允许git存档访问。☹️

其他回答

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

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

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

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

如果您想使用Python和SVN下载特定的GitHub目录,请使用以下代码:

import validators
from svn.remote import RemoteClient

def download_folder(url):
    if 'tree/master' in url:
        url = url.replace('tree/master', 'trunk')

    r = RemoteClient(url)
    r.export('output')


if __name__ == '__main__':
    url = input('Enter folder URL: ')
    if not validators.url(url):
        print('Invalid url')
    else:
        download_folder(url)

您可以在本教程中查看有关此代码和其他GitHub搜索和下载提示的更多详细信息:https://python.gotrained.com/search-github-api/

一个简单的答案是从下面的链接中选择第一个乌龟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中将github替换为githubbox,打开repo到codesandbox,然后在codesandbox上转到文件菜单并将其导出为zip。

对于以下回购:https://github.com/geist-org/react/tree/master/examples/custom-themes

输入以下url:https://githubbox.com/geist-org/react/tree/master/examples/custom-themes

在codesandbox中,转到文件菜单并将其导出为Zip。

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