Download a single folder or directory from a GitHu

2019-01-01 11:16发布

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.

标签: git github
26条回答
长期被迫恋爱
2楼-- · 2019-01-01 12:23

None of the answers helped in my situation. If you are developing for Windows, you likely don't have svn. In many situations one can't count on users to have Git installed either, or don't want to download entire repositories for other reasons. Some of the people that answered this question, such as Willem van Ketwich and aztack, made tools to accomplish this task. However, if the tool isn't written for the language you are using, or you don't want to install a third party library, these don't work.

However, there is a much easier way. GitHub has an API that allows you to download a single file or an entire directory's contents using GET requests. You can access a directory using https://api.github.com/repos/:owner/:repo_name/contents/:path that returns a JSON object enumerating all the files in the directory. Included in the enumeration is a link to the raw content of the file, the download_url parameter. The file can then be downloaded using that URL.

It's a two step process that requires the ability to make GET requests, but this can be implemented in pretty much any language, on any platform. It can be used to get files or directories.

查看更多
何处买醉
3楼-- · 2019-01-01 12:23

Use this function, the first argument is the url to the folder, the second is the place the folder will be downloaded to:

function github-dir() {
    svn export "$(sed 's/tree\/master/trunk/' <<< "$1")" "$2"  
}
查看更多
登录 后发表回答