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 Sep. 2016: there are a few tools created by the community that can do this for you:
GitZip (Credits to Kino - upvote his answer right here!)
DownGit (Credits to Minhas Kamal - upvote his answer right here!)
Git doesn't support this, but Github does 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.Get the repo URL. First, copy the URL of the Github repo to your clipboard.
Modify the URL for subversion. I want to download the folder at
/docs
from themaster
branch, so I will appendtrunk/docs
. Full URL is nowhttps://github.com/lodash/lodash/trunk/docs
. See my note below for a more in-depth explanation of why we must use this URL format.Download the folder. Go to the command line and grab the folder with SVN.
svn checkout https://github.com/lodash/lodash/trunk/docs
You might not see any activity immediately because Github takes up to 30 seconds to convert larger repositories, so be patient.
That's all! Github supports more subversion features as well, including support for committing and pushing changes.
I wrote a tool with Node.js just for this. Check it out Download Repo Dir
install with
npm i -g dl-repo-dir
and commandrepo
will be available globally.download and rename a directory in a repository
repo download aztack/download-repo-dir lib src/lib/new-name
download a repository
repo download aztack/download-repo-dir '' src/lib/download-repo-dir
download from a private gitlab repository with given tag
export GITLAB_API_PRIVATE_TOKEN=YOUR_TOKEN_HERE
repo download gitlab:mygitlab.com:topgroup/subgroup/repo#v1.0.0 dir src/lib/new-name
and there will be a
repo.json
file to save all the information.In a new project, you can initialize the project with exists
repo.json
usingrepo init
command.I use linux so , put this in ~/.bashrc , called even :D $HOME/.bashrc
then refresh the shell with
then use it with git-downloadfolder blablabla :D
Two options for this feature:
Option 1: Browser Extensions
Usage:
Get Token:
Option 2: Github gh-page
Step1: Input github url to the field at the top-right.
Step2: Press enter or click download for download zip directly or click search for view the list of sub-folders and files.
Step3: Click "Download Zip File" or "Get File" button to get files.
In most cases, it works fine, except that the folder contains more than 1,000 files, because of the Github Trees API limitation. (refers to Github API#Contents)
And it also can support private/public repos and upgrade the rate limit, if you have GitHub account and use "get token" link in this site.
I work with CentOS 7 servers on which I don't have root access, nor git, svn, etc (nor want to!) so made a python script to download any github folder: https://github.com/andrrrl/github-folder-downloader
Usage is simple, just copy the relevant part from a github project, let's say the project is https://github.com/MaxCDN/php-maxcdn/, and you want a folder where some source files are only, then you need to do something like:
$ python gdownload.py "/MaxCDN/php-maxcdn/tree/master/src" /my/target/dir/
(will create target folder if doesn't exist)
It requires lxml library, can be installed with
easy_install lxml
If you don't have root access (like me) you can create a
.pydistutils.py
file into your$HOME
dir with these contents:[install] user=1
Andeasy_install lxml
will just work (ref: https://stackoverflow.com/a/33464597/591257).If you are comfortable with unix commands, you don't need special dependencies or web apps for this. You can download the repo as a tarball and untar only what you need.
Example (woff2 files from a subdirectory in fontawesome):
*/
) to match any directory. Github creates a wrapper directory with the commit ref in the name, so it can't be known.--strip-components
to be the same as the amount of slashes (/
) in the path (previous argument).This will download the whole tarball. Use the SVN method mentioned in the other answers if this has to be avoided or if you want to be nice to the GitHub servers.