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

举个GitHub repo的例子:

git@github.com:foobar/Test.git

其目录结构:

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

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


当前回答

这是SVN优于Git的少数几个地方之一。

最终,我们倾向于三种选择:

使用wget从GitHub获取数据(使用原始文件视图)。让上游项目将所需的数据子集发布为构建工件。放弃并使用全额结账。它在第一个版本中大受欢迎,但除非您获得大量流量,否则在接下来的版本中不会太麻烦。

其他回答

要从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存储库文件夹下载到当前目录并删除git文件。

#!/bin/sh    

function download_git_folder() {
  repo_url=$1
  branch=$2
  repo_subfolder_path=$3
  
  repo_folder=$(basename $repo_url)
  git init
  git remote add -f origin ${repo_url}
  git config core.sparseCheckout true
  echo "${repo_subfolder_path}" >> .git/info/sparse-checkout
  git pull origin ${branch}
  mv "${repo_subfolder_path}"/* ./

  readarray -td/ root_subfolder <<<"${repo_subfolder_path}"; declare -p root_subfolder;
  rm -rf ./.git ${root_subfolder[0]}
}

用法

download_git_folder "git@github.com:foobar/Test.git" "master" "Test/bar" 

只需5步

从这里下载SVN。打开CMD并转到SVN bin目录,如:cd%ProgramFiles%\SlikSvn\bin假设我想下载这个目录URLhttps://github.com/ZeBobo5/Vlc.DotNet/tree/develop/src/Samples代替树/开发或树干的树/主现在启动最后一个命令以下载同一目录中的文件夹。

svn export https://github.com/ZeBobo5/Vlc.DotNet/trunk/src/Samples

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

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

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

此功能有两个选项:

选项1:GitZip浏览器扩展

Chrome扩展、Edge扩展、Firefox插件

用法:

浏览任何Github存储库页面。两种下载方式:选择项目:默认情况下,您可以双击项目或选中项目前面的复选框。单击页面右下角的下载按钮。在上下文菜单中:单击“GitZip下载”>“整个存储库”或“当前文件夹”。将鼠标光标移动到项目上,然后单击“GitZip下载”>“选定文件夹/文件”。完成2-1-1后,单击“GitZip下载”>“已检查项目”。查看进度仪表板并等待浏览器触发器下载。获取ZIP文件。

获取令牌:

单击浏览器上的GitZip Extension图标。单击“获取令牌”之外的“普通”或“专用”链接。在Github身份验证页面上授权GitZip权限。返回到开始的回购页面。继续使用。


选项2:Github gh页面

http://kinolien.github.io/gitzip通过使用GitHub API和JSZip、FileSaver.js库。

步骤1:在右上角的字段中输入github url。步骤2:按回车键或直接单击下载以下载zip,或单击搜索以查看子文件夹和文件的列表。步骤3:单击“下载Zip文件”或“获取文件”按钮获取文件。

在大多数情况下,它工作正常,除了文件夹包含1000多个文件,因为Github Trees API的限制。(参考Github API#内容)

此外,如果您拥有GitHub帐户并在该站点中使用“获取令牌”链接,它还可以支持私有/公共回购并升级费率限制。