如何从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文件夹,而不是克隆整个测试项目。
当前回答
试试看。
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"
其他回答
为了放大上面的答案,一个从真正的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
有时,一个具体的例子有助于澄清所提出的替代方案。
要从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
如果您有svn,可以使用svn导出来执行以下操作:
svn export https://github.com/foobar/Test.git/trunk/foo
请注意URL格式:
基本URL为https://github.com//末尾附加的树干
在运行svn导出之前,最好先使用以下命令验证目录的内容:
svn ls https://github.com/foobar/Test.git/trunk/foo
为了独特,我必须说,你也可以在没有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
注:我是作者。如果有任何遗漏或不清楚的地方,您可以发表评论。
您可以简单地下载目录树:
git archive --remote git@github.com:foobar/Test.git HEAD:foo | tar xf -
但如果你想检查一下,并且能够提交并将它们推回去,那么你就不能这样做。