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

举个GitHub repo的例子:

git@github.com:foobar/Test.git

其目录结构:

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

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


当前回答

在尝试了所有答案后,对我来说最好的解决方案是:

GitHub基于vscode的编辑器。

赞成的意见:

不需要任何额外的工具,如svn或API令牌。内容大小无限制另存为目录或文件,而不是存档。

说明书

转到任何回购。(例如。https://github.com/RespiraWorks/Ventilator/tree/master/software)按或者在URL中将.com替换为.dev,以在GitHub的内部编辑器中打开repo在资源管理器窗格(左侧或按Ctrl+Shift+E)中,右键单击所需的文件/文件夹并选择下载。在“选择文件夹”对话框中,选择磁盘上希望所选文件/文件夹存在的目录。

Note

我尝试了其他解决方案,如接受的答案,

不要只为此安装和学习svn。其他工具,如Download Directory、Refined GitHub、GitZip和DownGit,要么需要API令牌,要么无法下载大型目录。

其他选项

带有远程存储库扩展名的VSCode,用于打开存储库并下载文件/文件夹。

其他回答

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

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

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

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

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

最简单的方法是使用fetcher

首先,使用以下命令安装fetcher:

npm安装-ggithub文件获取器

然后您可以下载带有URL的文件或文件夹:

fetcher--url=resource_url--out=output_directory

例如:

fetcher--url=“https://github.com/Gyumeijie/github-files-fetcher/blob/master/CHANGELOG.md“--out=/tmp

通过在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存储库文件夹下载到当前目录并删除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"