Download a single folder or directory from a GitHub repo

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.


Solution 1:

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

  • Download Directory (Credits to fregante)
    • It has also been integrated into the excellent Refined Github chrome extension as a button in the Github web UI.
  • GitZip (Credits to Kino - see his answer here)
  • DownGit (Credits to Minhas Kamal - see his answer here)

Note: if you're trying to download a large number of files, you may need to provide a token to these tools to avoid rate limiting.


Original (manual) approach: Checking out an individual directory is not supported by git natively, but Github can do this 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.

Solution 2:

Go to DownGit > Enter Your URL > Download!

You can DIRECTLY DOWNLOAD or create DOWNLOAD LINK for any GitHub public directory or file from DownGit-


DownGit


You may also configure properties of the downloaded file- detailed usage.


Disclaimer: I fell into the same problem as the question-asker and could not find any simple solution. So, I developed this tool for my own use first, then opened it for everyone :)

Solution 3:

Two options for this feature:

Option 1: GitZip Browser Extension

Chrome Extension, Firefox Addon

Usage:

  1. In any GitHub repos page.
  2. Just double click on the blank part of the items you need.
  3. Click download button at bottom-right.
  4. See the progress dashboard and wait for browser trigger download.
  5. Get the ZIP file.

Get Token:

  1. Click GitZip Extension icon on your browser.
  2. Click "Normal" or "Private" link besides "Get Token".
  3. Authorize GitZip permission on Github auth page.
  4. Back to repo page of the beginning.
  5. Continue to use.

Option 2: Github gh-page

http://kinolien.github.io/gitzip by using GitHub API, and JSZip, FileSaver.js libraries.

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.

Solution 4:

If you have svn, you can use svn export to do this:

svn export https://github.com/foobar/Test.git/trunk/foo

Notice the URL format:

  • The base URL is https://github.com/
  • /trunk appended at the end

Before you run svn export, it's good to first verify the content of the directory with:

svn ls https://github.com/foobar/Test.git/trunk/foo