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

举个GitHub repo的例子:

git@github.com:foobar/Test.git

其目录结构:

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

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


当前回答

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

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

其他回答

将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" 

如果要下载的目录是一个单独的库,最好创建其他git repo,然后使用git子模块函数。

当然,你必须是你想要的初始回购的所有者

git稀疏校验

Git 2.25.0包含了一个新的实验性Git稀疏签出命令,它使现有功能更易于使用,同时还为大型存储库带来了一些重要的性能优势。(GitHub博客)

当前版本示例:

git clone --filter=blob:none --sparse https://github.com/git/git.git
cd git
git sparse-checkout init --cone
git sparse-checkout add t

最值得注意的是

--稀疏仅将git存储库的顶级目录文件签入工作副本git稀疏签出add t增量添加/签出git的t子文件夹

其他要素

git稀疏签出init做了一些准备以启用部分签出--filter=blob:none通过只下载必要的git对象来优化数据获取(查看部分克隆功能以了解更多信息)--cone还通过应用更受限制的文件包含模式来提高性能


GitHub状态

GitHub仍在内部评估这一功能,尽管它在少数几个存储库上启用了[…]。随着该功能的稳定和成熟,我们将不断更新其进展。(文档)

一个简单的答案是从下面的链接中选择第一个乌龟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文件夹中。

最简单的方法是使用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