如何从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文件夹,而不是克隆整个测试项目。
当前回答
使用此函数,第一个参数是文件夹的url,第二个参数是下载文件夹的位置:
function github-dir() {
svn export "$(sed 's/tree\/master/trunk/' <<< "$1")" "$2"
}
其他回答
通过在url中将github替换为githubbox,打开repo到codesandbox,然后在codesandbox上转到文件菜单并将其导出为zip。
对于以下回购:https://github.com/geist-org/react/tree/master/examples/custom-themes
输入以下url:https://githubbox.com/geist-org/react/tree/master/examples/custom-themes
在codesandbox中,转到文件菜单并将其导出为Zip。
你不能;不同于Subversion,每个子目录都可以单独检出,Git在整个存储库的基础上运行。
对于需要更细粒度访问的项目,可以使用子模块——每个子模块都是一个单独的Git项目,因此可以单独克隆。
可以想象,Git前端(例如GitHub的web界面或gitweb)可以选择为您提供一个界面来提取给定的文件夹,但据我所知,他们中没有一个这样做(尽管他们确实允许您下载单个文件,所以如果文件夹不包含太多文件,这是一个选项)
编辑-GitHub实际上提供了通过SVN的访问,这将允许您执行此操作(根据评论)。看见https://github.com/blog/1438-improved-svn-here-to-stay-old-svn-going-away有关如何执行此操作的最新说明
如果在特殊情况下需要存储库中的某个文件,则会出现此问题。
这里可以找到一个简短的答案。您应该将url更改为以下格式:
https://raw.github.com/user/repository/branch/file.name
简单地解释一下,从github输入您想要的url。生吃。在url地址中的github之前,并从地址中删除blob。例如,假设您想要获取此地址中的csv文件:
https://github.com/CSSEGISandData/COVID-19/blob/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv
您应该将url更改为以下url:
https://raw.github.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv
您可以将ghget与从地址栏复制的任何URL一起使用:
ghget https://github.com/fivethirtyeight/data/tree/master/airline-safety
这是一个独立的可移植shell脚本,不使用SVN(这对我来说在大型回购中不起作用)。它也不使用API,因此不需要令牌,也不受速率限制。
免责声明:我做到了。
将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"