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

举个GitHub repo的例子:

git@github.com:foobar/Test.git

其目录结构:

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

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


当前回答

另一个具体示例:

就像我想从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中。

其他回答

在要加载的目录中:

git init
git remote add origin -f repoUrl // folder url
touch .git/info/sparse-checkout
git pull origin master

只有4行代码

无论谁在处理特定的文件夹,他都需要克隆该特定的文件夹本身,为此,请使用稀疏签出执行以下步骤。

创建目录。初始化Git存储库。(git初始化)启用稀疏检出。(git-config-core.sparsecheckout true)告诉Git你想要哪些目录(echo 2015/brand/May(参考你想要处理的文件夹)>>.Git/info/spease checkout)添加remote(gitremoteadd-f源https://jafartke.com/mkt-imdev/DVM.git)获取文件(git pull-origin master)

如果您有svn,可以使用svn导出来执行以下操作:

svn export https://github.com/foobar/Test.git/trunk/foo

请注意URL格式:

基本URL为https://github.com//末尾附加的树干

在运行svn导出之前,最好先使用以下命令验证目录的内容:

svn ls https://github.com/foobar/Test.git/trunk/foo

gitclone--筛选器仅下载所需文件

例如,要仅克隆此存储库的子目录big/所需的对象:https://github.com/cirosantilli/test-git-partial-clone-big-small我可以做到:

git clone --depth 1 --filter=blob:none --sparse \
  https://github.com/cirosantilli/test-git-partial-clone-big-small
cd test-git-partial-clone-big-small
git sparse-checkout set small

--filter选项是与远程协议的更新一起添加的,它确实防止了从服务器下载对象。

我在下面的文章中详细介绍了这一点:如何仅克隆Git存储库的子目录?

2021 1月在git 2.30.0上测试。

试试看。

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"