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.
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 theDownloads
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...
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:.git/
folder, or any untracked files in the repository it's run on.An example of creating an archive of the
docs/usage
directory from a remote repo you're connected to with ssh:More information in this blog post and the git documentation.
Note on GitHub Repos:
GitHub doesn't allow
git-archive
access. ☹️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:
run the following command:
It's one of the few places where SVN is better than Git.
In the end we've gravitated towards three options:
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
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
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.
Edited: If tree/master is not there in url then Fork it and it will be there in Forked url.