How can I download a specific folder from a GitHub repo?

How can I download a specific folder or directory from a remote Git repo hosted on GitHub? I only want just that a specific folder where the files for the front-end part of the app are kept, without having the other files as I don’t really need them.

16 Likes

If you are willing to use git to do this, you can do a sparse checkout.

We are going to start by creating an empty git repo locally, and then add the repository we want as a remote. This allows us to easily just checkout the folder we want

mkdir directoryName
cd directoryName
git init
git remote add origin -f repoUrl

After that, we want to add the directory to 

.git/info/sparse-checkout

(this is essentially the opposite of the gitignore file).

Then we can 

git pull origin master

and it should download only the folder you want. 

Obviously, this requires you to use git, but it shouldn’t bee too difficult to automate if it’s something you are doing often.

28 Likes

Awesome! Thanks a lot, will definitely try this.

I have a specific folder from a remote Git repo that I’ve hosted on GitHub. How can I download only that one specific folder?

@nadiajoyce Well this question is different mine is different, I want to download a specific folder locally not on a remote Git repo I think you misunderstood my question please check it once again.I totally understand that in order to maintain forum organized you have close my question but I request you to open again since my question is different from above question.

1 Like

If you look at the solution, it directly answers your question. The new topic you opened isn’t necessary and it is correct that people should be directed here.

You can try github-files-fetcher, it is a command line tool which downloads a single folder or file from a GitHub repo.

Think a real scenario: you were visiting the following webpage page and wanna download the async subdirectory alone.

With The github-files-fetcher, you should first copy the url of that page, which is https://github.com/reduxjs/redux/tree/master/examples/async, and then run the command below in command line:

fetcher --url=https://github.com/reduxjs/redux/tree/master/examples/async
2 Likes

Thanks, @mcinkay, this was really helpful for someone who’s still learning how to use Git/GitHub. Could you elaborate on how exactly to do this part?

After that, we want to add the directory to 

.git/info/sparse-checkout

(this is essentially the opposite of the gitignore file).

 

I’m essentially trying to download the Python-SQLite subfolder from the main code_snippets repo (see below), but I don’t know how exactly to add the directory to .git/info/sparse-checkout.

Python-SQLite subfolder: 

https://github.com/CoreyMSchafer/code_snippets/tree/master/Python-SQLite

code_snippets main repo:

https://github.com/CoreyMSchafer/code_snippets

Any help you can offer is greatly appreciated!

@gaylonalfano Edit the file and just put a line containing Python-SQLite The syntax is the same the gitignore file .

2 Likes

It still downloads everything.

19 Likes

Yep, it still downloads all

Nope there is a change.

mkdir <dir>

cd <dir>

git init

git remote add origin -f <URL>

git config core.sparseCheckout true # enable this

cat >> .git/info/sparsecheckout

<folder>

git pull origin master

Only your folder gets downloaded. 

This was tested on 2.13.3.windows worked fine.

14 Likes

For GitHub repos, you can clone any sub-directories of any GitHub repository (at any reference) using https://github.com/HR/github-clone

Hi Mclnkay,

I tried your command in the Bash (MacPro Terminal), am trying to pull/clone the specific folder from Github repo (Sub-folder under repo master folder) but it did not work, please advice, thank you

Alternatively, how would i be able to Copy-and-Paste a folder or file at local repo prior pull/clone its content-folder(or file) from remote Github? Thank you

Thanks, Weina

To add to this comment (particularly if you want to turn this series of commands into a script), I would amend it as follows:

mkdir <dir>

cd <dir>

git init

git remote add origin -f <URL>

git config core.sparseCheckout true # enable this

echo “/absolute/path/to/folder” > .git/info/sparse-checkout # if you don’t start with root you are liable to download multiple folders wherever the name you specified is matched.

git pull origin master

7 Likes

Github just need to implement 

https://minhaskamal.github.io/DownGit/#/home

It is just one button!

1 Like

I think the point when you do

git remote add origin -f url

everything is downloaded.

works! 

ty. 

Your solution is right, but the sequence of commands didn’t work for me in Windows. My version of git has no sparse-checkout command.

I am using the new PowerShell 7 command prompt https://aka.ms/powershell I never thought I’d say this of PowerShell, but it’s great! Should work in cmd too.

mkdir my_repo
cd my_repo
git init
git remote add origin<font color="#008000">[url]<br></font><font color="#333333">git config core.sparseCheckout true</font>echo<font color="#008000">[path-to-include]</font>> .git\info\sparse_checkout
git pull origin master

[url] is your remote git repo like https://github.com/hackf5/unityspy.git
[path-to-include] is the path that you want to include from the repo, see: https://git-scm.com/docs/git-sparse-checkout#_full_pattern_set

Yes, thanks.  Worked for me.  But, still very slow.  Not your fault!  (=:

Even faster:

USER=.....
PROJECT=.....
RELPATH=.....
BRANCH=master

svn export https://github.com/$USER/$PROJECT/branches/$BRANCH/$RELPATH

You need to install subversion as a prerequisite.

1 Like