如何从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文件夹,而不是克隆整个测试项目。
当前回答
通过在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。
其他回答
为了独特,我必须说,你也可以在没有svn、git或任何api的情况下下载Github文件夹。Github支持RAW链接,您可以利用该链接仅下载所需的文件和文件夹。
我注意到很多事情。以下是我的研究集:
机械装置
从网页中抓取所有超链接<a>并获取其href=“value”值如果href值包含“/tree/master/”或“/tree/main/”,则它是文件夹链接:https://github.com/graysuit/GithubFolderDownloader/tree/main/GithubFolderDownloader否则,如果href值包含“/bblo/master/”或“/bblo/main/”,则为文件链接:https://github.com/graysuit/GithubFolderDownloader/blob/main/GithubFolderDownloader.sln然后,将“github.com”替换为“raw.githubusercontent.com”,并从文件中删除“/blob/”:https://raw.githubusercontent.com/graysuit/GithubFolderDownloader/main/GithubFolderDownloader.sln它将成为RAW链接。现在你可以下载了。
Tool
在上述研究的基础上,我用C#创建了一个可以抓取文件夹的极简工具。graysuit/GithubFolderDownloader
注:我是作者。如果有任何遗漏或不清楚的地方,您可以发表评论。
如果您熟悉unix命令,则不需要特殊的依赖项或web应用程序。您可以将回购文件下载为tarball,并只下载您需要的内容。
示例(font真棒中的子目录中的woff2文件):
curl -L https://api.github.com/repos/FortAwesome/Font-Awesome/tarball | tar xz --wildcards "*/web-fonts-with-css/webfonts/*.woff2" --strip-components=3
有关链接格式的详细信息:https://developer.github.com/v3/repos/contents/#get-存档链接(包括如何获取zip文件或特定分支/refs)保留路径的初始部分(*/)以匹配任何目录。Github创建了一个包装器目录,名称中包含commit ref,因此无法知道。您可能希望--strip组件与路径中斜杠(/)的数量相同(上一个参数)。
这将下载整个tarball。如果必须避免这种情况,或者如果您想对GitHub服务器友好,请使用其他答案中提到的SVN方法。
为了放大上面的答案,一个从真正的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。
我找到的最简单的方法是如何仅克隆Git存储库的子目录?
我在gitclone命令中添加了-b<branch>--singlebranch以下载特定的分支。所以我使用的命令是:
git clone --depth 1 --single-branch -b <branch> --filter=blob:none --sparse <url>
git sparse-checkout set <directory>