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

举个GitHub repo的例子:

git@github.com:foobar/Test.git

其目录结构:

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

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


当前回答

要从GitHub导出目录,请将目录url中的“/tree/master/”替换为“trunk/”。

例如,要从以下URL导出目录:

https://github.com/liferay/liferay-plugins/tree/master/portlets/sample-hibernate-portlet

运行以下命令:

svn export https://github.com/liferay/liferay-plugins/trunk/portlets/sample-hibernate-portlet

其他回答

我使用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"); }'

无论谁在处理特定的文件夹,他都需要克隆该特定的文件夹本身,为此,请使用稀疏签出执行以下步骤。

创建目录。初始化Git存储库。(git初始化)启用稀疏检出。(git-config-core.sparsecheckout true)告诉Git你想要哪些目录(echo 2015/brand/May(参考你想要处理的文件夹)>>.Git/info/spease checkout)添加remote(gitremoteadd-f源https://jafartke.com/mkt-imdev/DVM.git)获取文件(git pull-origin master)

试试看。

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"

有一个名为githubdl的Python3pip包可以做到这一点*:

export GIT_TOKEN=1234567890123456789012345678901234567890123
pip install githubdl
githubdl -u http://github.com/foobar/test -d foo

项目页面在此处

*免责声明:这个包裹是我写的。