如何从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文件夹,而不是克隆整个测试项目。
当前回答
转到DownGit>输入您的URL>下载!
您可以直接下载或从DownGit为任何GitHub公共目录或文件创建下载链接-
您还可以配置下载文件的财产-详细用法。
免责声明:我和提问者陷入了同样的问题,无法找到任何简单的解决方案。因此,我首先开发了这个工具供自己使用,然后为每个人打开它:)
其他回答
如果您需要以编程方式执行,并且不想依赖SVN,则可以使用GitHubAPI递归下载所有内容。
为了获得灵感,以下是我的红宝石要点:https://gist.github.com/cvengros/b2a7e82f66519d423b6f
使用此函数,第一个参数是文件夹的url,第二个参数是下载文件夹的位置:
function github-dir() {
svn export "$(sed 's/tree\/master/trunk/' <<< "$1")" "$2"
}
将git存储库文件夹下载到当前目录并删除git文件。
#!/bin/sh
function download_git_folder() {
repo_url=$1
branch=$2
repo_subfolder_path=$3
repo_folder=$(basename $repo_url)
git init
git remote add -f origin ${repo_url}
git config core.sparseCheckout true
echo "${repo_subfolder_path}" >> .git/info/sparse-checkout
git pull origin ${branch}
mv "${repo_subfolder_path}"/* ./
readarray -td/ root_subfolder <<<"${repo_subfolder_path}"; declare -p root_subfolder;
rm -rf ./.git ${root_subfolder[0]}
}
用法
download_git_folder "git@github.com:foobar/Test.git" "master" "Test/bar"
这是我用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文档
您可以将ghget与从地址栏复制的任何URL一起使用:
ghget https://github.com/fivethirtyeight/data/tree/master/airline-safety
这是一个独立的可移植shell脚本,不使用SVN(这对我来说在大型回购中不起作用)。它也不使用API,因此不需要令牌,也不受速率限制。
免责声明:我做到了。