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

举个GitHub repo的例子:

git@github.com:foobar/Test.git

其目录结构:

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

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


当前回答

试试看。

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"

其他回答

为了放大上面的答案,一个从真正的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 repo,然后使用git子模块函数。

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

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

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

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

无论出于什么原因,svn解决方案对我来说都不起作用,而且由于我不需要svn来做任何其他事情,所以花时间尝试它是没有意义的,所以我使用我已经拥有的工具来寻找一个简单的解决方案。该脚本只使用curl和awk来下载GitHub目录中的所有文件,该目录描述为“/:user:repo/contents/:path”。

GitHub REST API调用的返回主体“GET/repos/:user:repo/contents/:path”命令返回一个对象,该对象包含目录中每个文件的“download_url”链接。

该命令行脚本使用curl调用REST API,并通过AWK发送结果,AWK过滤掉除“download_url”行之外的所有行,删除链接中的引号和逗号,然后使用另一个对curl的调用下载链接。

curl -s https://api.github.com/repos/:user/:repo/contents/:path | awk \
     '/download_url/ { gsub("\"|,", "", $2); system("curl -O "$2"); }'

转到DownGit>输入您的URL>下载!

您可以直接下载或从DownGit为任何GitHub公共目录或文件创建下载链接-



您还可以配置下载文件的财产-详细用法。


免责声明:我和提问者陷入了同样的问题,无法找到任何简单的解决方案。因此,我首先开发了这个工具供自己使用,然后为每个人打开它:)