git - 从 GitHub 存储库下载单个文件夹或目录

标签 git file github directory repository

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

假设 GitHub 存储库示例位于此处:

git@github.com:foobar/Test.git

其目录结构:

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

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

最佳答案

2021 年 4 月更新:社区创建的一些工具可以为您执行此操作:

注意:如果您尝试下载大量文件,您可能需要为这些工具提供 token 以避免速率限制。


原始(手动)方法: git 本身不支持 checkout 单个目录,但 Github 可以通过 SVN 执行此操作。如果您使用 subversion checkout 代码,Github 会在后端将 repo 从 git 转换为 subversion,然后提供请求的目录。

此处介绍了如何使用此功能下载特定文件夹。我将使用流行的 javascript 库 lodash 作为示例。

  1. 导航到您要下载的文件夹。让我们从 master 分支下载 /testgithub repo URL example

  2. 修改 URL 以进行颠覆。将 tree/master 替换为 trunk

    https://github.com/lodash/lodash/tree/master/test

    https://github.com/lodash/lodash/trunk/test

  3. 下载文件夹。转到命令行并使用 SVN 抓取文件夹。

svn checkout https://github.com/lodash/lodash/trunk/test

您可能不会立即看到任何事件,因为 Github 最多需要 30 秒来转换更大的存储库,所以请耐心等待。

Full URL format explanation:

  • If you're interested in master branch, use trunk instead. So the full path is trunk/foldername
  • If you're interested in foo branch, use branches/foo instead. The full path looks like branches/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/

相关文章:

git - 修复 fork 中损坏的主分支

joomla - 自动扩展更新无法处理来自 github 的 zip 文件?

git - 删除本地主分支

git - 使用 filezilla 中的 GIT

Git hook commit-msg git 添加文件

java - 如何打开网络上的 txt 文件并将其内容读取为字符串?

swift - 在 Swift 中从 NSBundle 获取文件路径

json - GitHub API 中的 blob_url、raw_url 和 contents_url 有什么区别?

混帐 : how to specify a default remote push-to branch?

file - 如何使用Flutter同步(不等待)从Assets文件夹中读取文件