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

举个GitHub repo的例子:

git@github.com:foobar/Test.git

其目录结构:

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

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


当前回答

对于通用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存档访问。☹️

其他回答

试试看。

https://github.com/twfb/git-directory-download

usage: gitd [-h] [-u URL] [-r] [-p] [--proxy PROXY]

optional arguments:
  -h, --help         show this help message and exit
  -u URL, --url URL  github url, split by ",", example: "https://x, http://y"
  -r, --raw          download from raw url
  -p, --parse        download by parsing html
  --proxy PROXY      proxy config, example "socks5://127.0.0.1:7891"

Example:
  1. download by raw url: gitd -u "https://github.com/twfb/git-directory-download"
  2. download by raw url: gitd -r -u "https://github.com/twfb/git-directory-download"
  3. dowmload by parsing: gitd -p -u "https://github.com/twfb/git-directory-download"
  4. download by raw url with proxy: gitd -r -u "https://github.com/twfb/git-directory-download" --proxy "socks5://127.0.0.1:7891"

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

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

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

你可以尝试为chrome使用gitzip扩展,它超级容易使用,而且以前也帮我下载了很多文件夹!

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

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

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

我们的团队为此编写了一个bash脚本,因为我们不想在我们的裸机服务器上安装SVN。

https://github.com/ojbc/docker/blob/master/java8-karaf3/files/git-download.sh

它使用github API,可以从如下命令行运行:

git-download.sh https://api.github.com/repos/ojbc/main/contents/shared/ojb-certs