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

举个GitHub repo的例子:

git@github.com:foobar/Test.git

其目录结构:

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

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


当前回答

我创建了一个简单的应用程序,支持下载目录、文件和存储库(私有/公共)。

应用程序:https://downdir.vercel.app/

github:https://github.com/renomureza/downdir

其他回答

另一个具体示例:

就像我想从url下载“iOS Pro Geo”文件夹

https://github.com/alokc83/APRESS-Books-Source-Code-/tree/master/%20Pro%20iOS%20Geo

我可以通过

svn checkout https://github.com/alokc83/APRESS-Books-Source-Code-/trunk/%20Pro%20iOS%20Geo

注意路径中的主干

编辑:(根据Tommie C的评论)

是的,使用导出而不是签出将提供一个干净的副本,而无需额外的git存储库文件。

svn export https://github.com/alokc83/APRESS-Books-Source-Code-/trunk/%20Pro%20iOS%20Geo

已编辑:如果树/主节点不在url中,则分叉它,它将在分叉的url中。

为了放大上面的答案,一个从真正的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存储库的子目录?

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

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

对于通用git Repo:

如果您想下载文件,而不是克隆具有历史记录的存储库,可以使用gitarchive来完成此操作。

gitarchive生成git存储库的压缩zip或tar存档。让它与众不同的一些东西:

您可以选择git存储库中要归档的文件或目录。它不会将.git/文件夹或其运行的存储库中任何未跟踪的文件归档。您可以归档特定的分支、标记或提交。使用git管理的项目通常使用它来生成项目版本(beta、release、2.0等)的存档,供用户下载。

使用ssh从远程存储库创建docs/usage目录归档的示例:

# in terminal
$ git archive --format tar --remote ssh://server.org/path/to/git HEAD docs/usage > /tmp/usage_docs.tar

更多信息请参阅本博客文章和git文档。

GitHub Repos注意事项:

GitHub不允许git存档访问。☹️

这是我用git v2.25.0做的,也是用v2.26.2测试的。这个技巧不适用于v2.30.1

TLDR

git clone --no-checkout --filter=tree:0 https://github.com/opencv/opencv
cd opencv

# requires git 2.25.x to 2.26.2
git sparse-checkout set data/haarcascades

您可以使用Docker来避免安装特定版本的git

git clone --no-checkout --filter=tree:0 https://github.com/opencv/opencv
cd opencv

# requires git 2.25.x to 2.26.2
docker run --rm -it -v $PWD/:/code/ --workdir=/code/ alpine/git:v2.26.2 sparse-checkout set data/haarcascades

完整解决方案

# bare minimum clone of opencv
$ git clone --no-checkout --filter=tree:0 https://github.com/opencv/opencv
...
Resolving deltas: 100% (529/529), done.

# Downloaded only ~7.3MB , takes ~3 seconds
# du = disk usage, -s = summary, -h = human-readable
$ du -sh opencv
7.3M    opencv/

# Set target dir
$ cd opencv
$ git sparse-checkout set data/haarcascades
...
Updating files: 100% (17/17), done.
# Takes ~10 seconds, depending on your specs

# View downloaded files
$ du -sh data/haarcascades/
9.4M    data/haarcascades/
$ ls data/haarcascades/
haarcascade_eye.xml                      haarcascade_frontalface_alt2.xml      haarcascade_licence_plate_rus_16stages.xml  haarcascade_smile.xml
haarcascade_eye_tree_eyeglasses.xml      haarcascade_frontalface_alt_tree.xml  haarcascade_lowerbody.xml                   haarcascade_upperbody.xml
haarcascade_frontalcatface.xml           haarcascade_frontalface_default.xml   haarcascade_profileface.xml
haarcascade_frontalcatface_extended.xml  haarcascade_fullbody.xml              haarcascade_righteye_2splits.xml
haarcascade_frontalface_alt.xml          haarcascade_lefteye_2splits.xml       haarcascade_russian_plate_number.xml

工具书类

git稀疏签出日志git稀疏签出文档gitfilter props文档