Learn how to clone a single folder of a github repository on any platform.

Download with Git

You can't do it. Unlike Subversion, where each subdirectory can be checked out individually, Git operates on a whole-repository basis. For projects where finer-grained access is necessary, you can use submodules -- each submodule is a separate Git project, and thus can be cloned individually.

But if a project wasn't configured so from the beggining, you're not able to do it.

Solution

To download a folder, we are not going to use Git, we are going to use SVN instead, yes ! Subversion will do the trick for you, just follow the following steps (you need to have svn installed on your machine and svn environment variable needs to be available in the command prompt):


1) If you are a windows user

If you don't have SVN installed on your machine, download Tortoise SVN and install in your desktop from here. Remember to enable the use of tortoise svn in the command line.
Once installed, try runing the comand :
svn

In your windows command prompt, if an alert says that svn is not defined, you need to declare a environment variable with the following value in the System Environment Variables, go to control panel > system and security > system > Environment variables > new

Environment variables

Name of variable : svn
##The path may vary, you need to give the path to TortoiseProc.exe in your desktop
Value of the variable : C:\Program Files\TortoiseSVN\bin\TortoiseProc.exe


Now we are going to simply use the svn checkout command from the command prompt, in this example, we are going to download the following folder from a xamarin repository. This repository has too many things that i don't need, and i cannot download a .zip file from this folder, therefore we are going to use subversion to clone only this folder.

Original url : https://github.com/xamarin/xamarin-forms-samples/tree/master/Todo

Now, the svn checkout command will expect :

# svn checkout 
svn checkout "https://github.com/xamarin/xamarin-forms-samples/tree/master/Todo"

But if you try to checkout with the normal URL, svn will not found any repo there. To fix this, you only need to replace tree/master from the url and replace it with trunk.

Final urlhttps://github.com/xamarin/xamarin-forms-samples/trunk/Todo

Finally, try executing the previous command with the final url :

svn checkout "https://github.com/xamarin/xamarin-forms-samples/trunk/Todo"

SVN Checkout for git

And everything should be now located in the folder where you executed the command.

Git svn clone folder repository

Final recommendations

  • If you use svn export <url> instead of svn checkout <url>, you'll get a clean copy of the folder without extra git repository files.
  • Use svn ls <url> to list all available tags and branches before downloading if you want.

Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors