如何从托管在 GitHub 上的远程 Git 存储库中仅下载特定文件夹或目录?
假设 GitHub 存储库示例位于此处:
git@github.com:foobar/Test.git
其目录结构:
Test/
├── foo/
│ ├── a.py
│ └── b.py
└── bar/
├── c.py
└── d.py
我只想下载 foo 文件夹,而不是克隆整个测试项目。
最佳答案
2021 年 4 月更新:社区创建的一些工具可以为您执行此操作:
- Download Directory (致谢fregante)
- 它也是integrated进优Refined Github chrome 扩展作为 Github 网络用户界面中的按钮。
- GitZip (致谢 Kino - 查看他的回答 here )
- DownGit (致谢 Minhas Kamal - 查看他的回答 here )
注意:如果您尝试下载大量文件,您可能需要为这些工具提供 token 以避免速率限制。
原始(手动)方法: git
本身不支持 checkout 单个目录,但 Github 可以通过 SVN 执行此操作。如果您使用 subversion checkout 代码,Github 会在后端将 repo 从 git 转换为 subversion,然后提供请求的目录。
此处介绍了如何使用此功能下载特定文件夹。我将使用流行的 javascript 库 lodash
作为示例。
修改 URL 以进行颠覆。将
tree/master
替换为trunk
。https://github.com/lodash/lodash/tree/master/test
➜https://github.com/lodash/lodash/trunk/test
下载文件夹。转到命令行并使用 SVN 抓取文件夹。
svn checkout https://github.com/lodash/lodash/trunk/test
您可能不会立即看到任何事件,因为 Github 最多需要 30 秒来转换更大的存储库,所以请耐心等待。
Full URL format explanation:
- If you're interested in
master
branch, usetrunk
instead. So the full path istrunk/foldername
- If you're interested in
foo
branch, usebranches/foo
instead. The full path looks likebranches/foo/foldername
- Protip: You can use
svn ls
to see available tags and branches before downloading if you wish
就是这样! Github supports more subversion features同样,包括对提交和推送更改的支持。
关于git - 从 GitHub 存储库下载单个文件夹或目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7106012/