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.
-
Navigate to the folder you want to download. Let's download
/test
frommaster
branch. -
Modify the URL for subversion. Replace
tree/master
withtrunk
.https://github.com/lodash/lodash/tree/master/test
➜https://github.com/lodash/lodash/trunk/test
-
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, usetrunk
instead. So the full path istrunk/foldername
- If you're interested in
foo
branch, usebranches/foo
instead. The full path looks likebranches/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-
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:
- In any GitHub repos page.
- Just double click on the blank part of the items you need.
- Click download button at bottom-right.
- See the progress dashboard and wait for browser trigger download.
- Get the ZIP file.
Get Token:
- Click GitZip Extension icon on your browser.
- Click "Normal" or "Private" link besides "Get Token".
- Authorize GitZip permission on Github auth page.
- Back to repo page of the beginning.
- 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