Download a single folder or directory from a GitHub repo

g_inherit picture g_inherit · Aug 18, 2011 · Viewed 800k times · Source

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:

[email protected]: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.

Answer

nick picture nick · Aug 12, 2013

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


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.

  1. Navigate to the folder you want to download. Let's download /test from master branch. github repo URL example

  2. Modify the URL for subversion. Replace tree/master with trunk.

    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 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.

Full URL format explanation:

  • If you're interested in master branch, use trunk instead. So the full path is trunk/foldername
  • If you're interested in foo branch, use branches/foo instead. The full path looks like branches/foo/foldername
  • Protip: You can use svn ls to see available tags and branches before downloading if you wish

That's all! Github supports more subversion features as well, including support for committing and pushing changes.