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

举个GitHub repo的例子:

git@github.com:foobar/Test.git

其目录结构:

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

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


当前回答

如果在特殊情况下需要存储库中的某个文件,则会出现此问题。

这里可以找到一个简短的答案。您应该将url更改为以下格式:

https://raw.github.com/user/repository/branch/file.name

简单地解释一下,从github输入您想要的url。生吃。在url地址中的github之前,并从地址中删除blob。例如,假设您想要获取此地址中的csv文件:

https://github.com/CSSEGISandData/COVID-19/blob/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv

您应该将url更改为以下url:

https://raw.github.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv

其他回答

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

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



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


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

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

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

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

为了放大上面的答案,一个从真正的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

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

通过在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。

如果您想使用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/