从 GitHub 存储库下载单个文件夹或目录 [英] Download a single folder or directory from a GitHub repo

查看:31
本文介绍了从 GitHub 存储库下载单个文件夹或目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

git@github.com:foobar/Test.git

其目录结构:

测试/├── foo/│ ├── a.py│ └── b.py└── 酒吧/├── c.py└── d.py

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

解决方案

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

  • 修改 subversion 的 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 秒来转换更大的存储库,所以请耐心等待.

    <块引用>

    完整的 URL 格式说明:

    • 如果您对 master 分支感兴趣,请改用 trunk.所以完整路径是trunk/foldername
    • 如果您对 foo 分支感兴趣,请改用 branches/foo.这完整路径看起来像 branches/foo/foldername
    • 提示:如果您愿意,您可以在下载前使用 svn ls 查看可用的标签和分支

    仅此而已!Github 也支持更多的颠覆功能,包括对提交和推送的支持变化.

    How can I download only a specific folder or directory from a remote Git repo hosted on GitHub?

    Say the example GitHub repo lives here:

    git@github.com:foobar/Test.git
    

    Its directory structure:

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

    I want to download only the foo folder and not clone the whole Test project.

    解决方案

    Update Apr. 2021: there are a few tools created by the community that can do this for you:

    Note: if you're trying to download a large number of files, you may need to provide a token to these tools to avoid rate limiting.


    Original (manual) approach: Checking out an individual directory is not supported by git natively, but Github can do this via SVN. If you checkout your code with subversion, Github will essentially convert the repo from git to subversion on the backend, then serve up the requested directory.

    Here's how you can use this feature to download a specific folder. I'll use the popular javascript library lodash as an example.

    1. Navigate to the folder you want to download. Let's download /test from master branch.

    2. Modify the URL for subversion. Replace tree/master with trunk.

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

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

    3. Download the folder. Go to the command line and grab the folder with SVN.

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

    You might not see any activity immediately because Github takes up to 30 seconds to convert larger repositories, so be patient.

    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

    That's all! Github supports more subversion features as well, including support for committing and pushing changes.

    这篇关于从 GitHub 存储库下载单个文件夹或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆