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:08

If you truly just want to just "download" the folder and not "clone" it (for development), the easiest way to simply get a copy of the most recent version of the repository (and therefore a folder/file within it), without needing to clone the whole repo or even install git in the first place, is to download a zip archive (for any repo, fork, branch, commit, etc.) by going to the desired repository/fork/branch/commit on GitHub (e.g. http(s)://github.com/<user>/<repo>/commit/<Sha1> for a copy of the files as they were after a specific commit) and selecting the Downloads button near the upper-right.

This archive format contains none of the git-repo magic, just the tracked files themselves (and perhaps a few .gitignore files if they were tracked, but you can ignore those :p) - that means that if the code changes and you want to stay on top, you'll have to manually re-download it, and it also means you won't be able to use it as a git repository...

Not sure if that's what you're looking for in this case (again, "download"/view vs "clone"/develop), but it can be useful nonetheless...

查看更多
琉璃瓶的回忆
3楼-- · 2019-01-01 12:10

For a Generic git Repo:

If you want to download files, not clone the repository with history, you can do this with git-archive.

git-archive makes a compressed zip or tar archive of a git repository. Some things that make it special:

  1. You can choose which files or directories in the git repository to archive.
  2. It doesn't archive the .git/ folder, or any untracked files in the repository it's run on.
  3. You can archive a specific branch, tag, or commit. Projects managed with git often use this to generate archives of versions of the project (beta, release, 2.0, etc.) for users to download.

An example of creating an archive of the docs/usage directory from a remote repo you're connected to with ssh:

# in terminal
$ git archive --format tar --remote ssh://server.org/path/to/git HEAD docs/usage > /tmp/usage_docs.tgz

More information in this blog post and the git documentation.

Note on GitHub Repos:

GitHub doesn't allow git-archive access. ☹️

查看更多
其实,你不懂
4楼-- · 2019-01-01 12:11

To export a directory from GitHub, replace "/tree/master/" in the directory's url with "/trunk/".

For example, to export the directory from the following URL:

https://github.com/liferay/liferay-plugins/tree/master/portlets/sample-hibernate-portlet

run the following command:

svn export https://github.com/liferay/liferay-plugins/trunk/portlets/sample-hibernate-portlet
查看更多
千与千寻千般痛.
5楼-- · 2019-01-01 12:13

It's one of the few places where SVN is better than Git.

In the end we've gravitated towards three options:

  1. Use wget to grab the data from GitHub (using the raw file view).
  2. Have upstream projects publish the required data subset as build artifacts.
  3. Give up and use the full checkout. It's big hit on the first build, but unless you get lot of traffic, it's not too much hassle in the following builds.
查看更多
十年一品温如言
6楼-- · 2019-01-01 12:15

If you need to do it programatically and you don't want to rely on SVN, you can use GitHub API to download all the contents recursively.

For inspiration, here's my ruby gist: https://gist.github.com/cvengros/b2a7e82f66519d423b6f

查看更多
何处买醉
7楼-- · 2019-01-01 12:16

Another specific example:

Like I want to download 'iOS Pro Geo' folder from the url

https://github.com/alokc83/APRESS-Books-Source-Code-/tree/master/%20Pro%20iOS%20Geo

and I can do so via

svn checkout https://github.com/alokc83/APRESS-Books-Source-Code-/trunk/%20Pro%20iOS%20Geo

Note trunk in the path

Edited: (as per Tommie C's comment)

Yes, using export instead of checkout would give a clean copy without extra git repository files.

svn export https://github.com/alokc83/APRESS-Books-Source-Code-/trunk/%20Pro%20iOS%20Geo

Edited: If tree/master is not there in url then Fork it and it will be there in Forked url.

查看更多
登录 后发表回答