如何从GitHub上托管的远程Git repo中仅下载特定文件夹或目录?
举个GitHub repo的例子:
git@github.com:foobar/Test.git
其目录结构:
Test/
├── foo/
│ ├── a.py
│ └── b.py
└── bar/
├── c.py
└── d.py
我只想下载foo文件夹,而不是克隆整个测试项目。
如何从GitHub上托管的远程Git repo中仅下载特定文件夹或目录?
举个GitHub repo的例子:
git@github.com:foobar/Test.git
其目录结构:
Test/
├── foo/
│ ├── a.py
│ └── b.py
└── bar/
├── c.py
└── d.py
我只想下载foo文件夹,而不是克隆整个测试项目。
当前回答
我使用linux,所以把它放在~/.bashrc中,甚至称为:D$HOME/.bashrc
git-dowloadfolder(){
a="$1"
svn checkout ${a/tree\/master/trunk}
}
然后用
source ~/.bashrc
然后将其与git downloadfolder blablabla:D一起使用
其他回答
无论出于什么原因,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"); }'
此功能有两个选项:
选项1:GitZip浏览器扩展
Chrome扩展、Edge扩展、Firefox插件
用法:
浏览任何Github存储库页面。两种下载方式:选择项目:默认情况下,您可以双击项目或选中项目前面的复选框。单击页面右下角的下载按钮。在上下文菜单中:单击“GitZip下载”>“整个存储库”或“当前文件夹”。将鼠标光标移动到项目上,然后单击“GitZip下载”>“选定文件夹/文件”。完成2-1-1后,单击“GitZip下载”>“已检查项目”。查看进度仪表板并等待浏览器触发器下载。获取ZIP文件。
获取令牌:
单击浏览器上的GitZip Extension图标。单击“获取令牌”之外的“普通”或“专用”链接。在Github身份验证页面上授权GitZip权限。返回到开始的回购页面。继续使用。
选项2:Github gh页面
http://kinolien.github.io/gitzip通过使用GitHub API和JSZip、FileSaver.js库。
步骤1:在右上角的字段中输入github url。步骤2:按回车键或直接单击下载以下载zip,或单击搜索以查看子文件夹和文件的列表。步骤3:单击“下载Zip文件”或“获取文件”按钮获取文件。
在大多数情况下,它工作正常,除了文件夹包含1000多个文件,因为Github Trees API的限制。(参考Github API#内容)
此外,如果您拥有GitHub帐户并在该站点中使用“获取令牌”链接,它还可以支持私有/公共回购并升级费率限制。
这是我用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文档
为了独特,我必须说,你也可以在没有svn、git或任何api的情况下下载Github文件夹。Github支持RAW链接,您可以利用该链接仅下载所需的文件和文件夹。
我注意到很多事情。以下是我的研究集:
机械装置
从网页中抓取所有超链接<a>并获取其href=“value”值如果href值包含“/tree/master/”或“/tree/main/”,则它是文件夹链接:https://github.com/graysuit/GithubFolderDownloader/tree/main/GithubFolderDownloader否则,如果href值包含“/bblo/master/”或“/bblo/main/”,则为文件链接:https://github.com/graysuit/GithubFolderDownloader/blob/main/GithubFolderDownloader.sln然后,将“github.com”替换为“raw.githubusercontent.com”,并从文件中删除“/blob/”:https://raw.githubusercontent.com/graysuit/GithubFolderDownloader/main/GithubFolderDownloader.sln它将成为RAW链接。现在你可以下载了。
Tool
在上述研究的基础上,我用C#创建了一个可以抓取文件夹的极简工具。graysuit/GithubFolderDownloader
注:我是作者。如果有任何遗漏或不清楚的地方,您可以发表评论。
试试看。
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"