Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

Say the example GitHub repo lives here:

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

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.

(我只想下载foo文件夹,而不克隆整个Test项目。)

  ask by g_inherit translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
432 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

(2016年9月更新:社区创建了一些工具可以为您做到这一点:)


Git doesn't support this, but Github does via SVN.

(Git不支持此功能,但是Github通过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.

(如果您使用subversion签出代码,Github实际上将在后端将仓库从git转换为subversion,然后提供所请求的目录。)

Here's how you can use this feature to download a specific folder.

(这是使用此功能下载特定文件夹的方法。)

I'll use the popular javascript library lodash as an example.

(我将以流行的javascript库lodash为例。)

  1. Navigate to the folder you want to download .

    (导航到要下载的文件夹 。)

    Let's download /test from master branch.

    (让我们从master分支下载/test 。) github repo URL示例

  2. Modify the URL for subversion .

    (修改用于Subversion的URL 。)

    Replace tree/master with trunk .

    (将tree/master替换为trunk 。)

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

    (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抓取文件夹。)

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.

(您可能不会立即看到任何活动,因为Github最多需要30秒来转换较大的存储库,因此请耐心等待。)

Full URL format explanation:

(完整的URL格式说明:)

  • If you're interested in master branch, use trunk instead.

    (如果您对master分支感兴趣,请改用trunk 。)

    So the full path is trunk/foldername

    (所以完整路径是trunk/foldername)

  • If you're interested in foo branch, use branches/foo instead.

    (如果您对foo分支感兴趣,请改用branches/foo 。)

    The full path looks like branches/foo/foldername

    (完整路径看起来像branches/foo/foldername)

  • Protip: You can use svn ls to see available tags and branches before downloading if you wish

    (提示:如果愿意,可以在下载前使用svn ls查看可用的标签和分支)

That's all!

(就这样!)

Github supports more subversion features as well, including support for committing and pushing changes.

(Github还支持更多的Subversion功能 ,包括对提交和推送更改的支持。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...