Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 452
g_inherit Avatar asked Aug 18 '11 10:08

g_inherit


People also ask

How do I download a single directory from git?

To export just one folder, you just add the /trunk followed by the desired path, like svn export https://github.com/RobTillaart/Arduino.git/trunk/libraries/DHTlib .

How do I download a directory from GitHub terminal?

Open up Git Bash, type in “cd Downloads” and hit Enter. This will take you to the Downloads folder in the command window, you can also type whatever file location you want to save the file in. Now, type in “git clone https://github.com/bdward16/tip-calculator.git“and hit Enter.

How do I download individual files from repository?

Downloading a Single File From The Github Website You can copy/paste from here, but in most browsers, you should be able to right click and select “Save As” to download the file directly. For code files, it may try to save as . txt , which you will need to fix manually before or after downloading.


1 Answers

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.

like image 51
nick Avatar answered Oct 27 '22 07:10

nick