如何从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文件夹,而不是克隆整个测试项目。
当前回答
如果您需要以编程方式执行,并且不想依赖SVN,则可以使用GitHubAPI递归下载所有内容。
为了获得灵感,以下是我的红宝石要点:https://gist.github.com/cvengros/b2a7e82f66519d423b6f
其他回答
使用此函数,第一个参数是文件夹的url,第二个参数是下载文件夹的位置:
function github-dir() {
svn export "$(sed 's/tree\/master/trunk/' <<< "$1")" "$2"
}
如果要下载的目录是一个单独的库,最好创建其他git repo,然后使用git子模块函数。
当然,你必须是你想要的初始回购的所有者
如果您想使用Python和SVN下载特定的GitHub目录,请使用以下代码:
import validators
from svn.remote import RemoteClient
def download_folder(url):
if 'tree/master' in url:
url = url.replace('tree/master', 'trunk')
r = RemoteClient(url)
r.export('output')
if __name__ == '__main__':
url = input('Enter folder URL: ')
if not validators.url(url):
print('Invalid url')
else:
download_folder(url)
您可以在本教程中查看有关此代码和其他GitHub搜索和下载提示的更多详细信息:https://python.gotrained.com/search-github-api/
为了放大上面的答案,一个从真正的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
有时,一个具体的例子有助于澄清所提出的替代方案。
您可以将ghget与从地址栏复制的任何URL一起使用:
ghget https://github.com/fivethirtyeight/data/tree/master/airline-safety
这是一个独立的可移植shell脚本,不使用SVN(这对我来说在大型回购中不起作用)。它也不使用API,因此不需要令牌,也不受速率限制。
免责声明:我做到了。