How to download a single file or a subfolder from a Github repository


 24 Nov, 2015  doru  642  
github svn

If you want to download an entire project from Github the things are very straightforward: you can clone it on your own computer with git or, even simpler and without requiring git, you can press the Download ZIP button and you'll have the project archive on your computer.

But what if you want to download a single subfolder of the project or a single file from this? Github doesn't provide a way to do this. And this is also impossible to be done using git. Of course, you can download the entire project archive, unzip it and then take out the subfolder or file you need and delete the unneeded files and folders. But this looks tedious and a bit inefficent. There isn't other simpler way to do this?

Yes, there is! But for this you need to have installed - or to install - svn. (If you are running any flavor of GNU Linux it's possible you have it already installed.)

Download a single file from a Github repository

So, let's say you want to download a single file from Github, for example the file at the following URL:

https://github.com/codingforentrepreneurs/Try-Django-1.8/blob/master/src/templates/400.html

using svn you should run the command (in the terminal):

svn export https://github.com/codingforentrepreneurs/Try-Django-1.8/trunk/src/templates/400.html

obviously, before running the command you should navigate in terminal to the folder in which you want to download the file. After running the command you should have the 400.html file in that folder. It has to be noted that the blob/master part of the original URL must be replaced with trunk in the svn command, for this to work.

Download a subfolder from a Github repository

As you may guess, to download a subfolder instead of a single file from a Github repository you can use the same command but this time the URL used in svn should end with the name of the subfolder to be downloaded: For example, if the Github URL of this folder is

https://github.com/codingforentrepreneurs/Try-Django-1.8/tree/master/src/templates/registration

you run the svn command

svn export https://github.com/codingforentrepreneurs/Try-Django-1.8/trunk/src/templates/registration

to download it. Note the same replacement of - here - tree/master with trunk. This must be done, otherwise the command won't work.

All these being said (or rather, written) it only remains me to say - merry (ro)coding everyone!