2002

How can I download only a specific folder or directory from a remote Git repository hosted on GitHub?

Say the example GitHub repository 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.

9
  • 13
    I wrote a shell script for this using svn checkout - github.com/Decad/github-downloader Commented Nov 5, 2013 at 11:01
  • 867
    Can somebody explain to me why there isn't an "official" web-interface solution to this? It seems like a trivial feature to implement, yet super useful.
    – Alexander
    Commented Jul 14, 2014 at 5:02
  • 5
    I've created an open source project, called GitHubFolderDownloader. It lets you to download a single folder of a repository without cloning or downloading the whole repository.
    – VahidN
    Commented Jun 17, 2015 at 10:16
  • 5
    see @janos answer svn alternative is the best, no auth, no registration, no limit, no plugins
    – stackdave
    Commented Oct 11, 2017 at 10:30
  • 11
    @Alexander There is an official way. Check my answer stackoverflow.com/a/70729494/11218031 Commented Jan 22, 2022 at 6:25

48 Answers 48

1782

Update April 2021: there are a few tools created by the community that can do this for you:

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 Subversion (SVN). If you checkout your code with Subversion, GitHub will essentially convert the repository from Git to Subversion on the backend, and 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 repository 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
  • Pro tip: 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.

33
  • 17
    I modified this to use svn export, as I didn't want a Subversion working copy. Then I added the resulting folder in Git. (I somehow lost a large piece of my directory tree, so I exported from the repo I forked.)
    – Grault
    Commented Dec 18, 2013 at 18:34
  • 8
    @num3ric- sorry for the late reply, but you should be able to replace "trunk" with "branches/yourbranch"
    – nick
    Commented Oct 25, 2014 at 21:49
  • 5
    Still works great as of 2020/6/18 - downloaded a particular folder in a given branch on Windows 10 using TortoiseSVN -> Export feature (available right clicking on any File Explorer directory), Used this URL: https://github.com/$organization/$repo/branches/$branch/$directory Commented Jun 18, 2020 at 11:11
  • 10
    As of 28th October 2020, if you attempt the method repo/branches/foo_branch/bar_folder you will receive a status 410, feature gone.
    – Hugo
    Commented Oct 28, 2020 at 14:59
  • 12
    GitHub is ending support of SVN on January 8, 2024 github.blog/2023-01-20-sunsetting-subversion-support
    – Hugo
    Commented Jan 27, 2023 at 20:13
1144

Go to DownGit → Enter Your URL → Download!

You can directly download or create download link for any GitHub public directory or file from DownGit:


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, and then opened it for everyone :)

18
  • 29
    The base method is- 1. take the directory link (used angular-route), 2. go to GitHub API, 3. search through the directory and download all files, 4. create a zip file with it (I have used jszip) & save it as file (used filesaver for this). For clear understanding I would suggest to take a look in my project. Commented Aug 20, 2016 at 10:06
  • @MYounas Sorry I cannot suggest you any tutorial as I did not follow any. You may get some help from here. Commented Aug 20, 2016 at 12:51
  • 14
    This is cool but it doesn't work with automated tools (like wget)
    – Ring
    Commented Jan 25, 2018 at 21:10
  • 5
    DownGit was exactly what I needed: To download a specific folder from a repo without creating a new git directory in order to do so (like the subversion method requires). Thanks very much for making this. Commented May 10, 2022 at 5:09
  • 14
    The website got blacklisted by my antivirus... Commented Aug 19, 2023 at 20:34
693
+200

Two options for this feature:

Option 1: GitZip Browser Extension

Chrome Extension, Edge Extension, Firefox Addon

Usage:

  1. Browse any GitHub repository page.
  2. Two ways to download:
    1. Choose the items:
      1. In default, you can double click on items or check the checkbox on the front of items.
      2. Click the download button at the bottom-right of the page.
    2. In the context menu:
      1. Click "GitZip Download" → "Whole Repository" or "Current Folder".
      2. Move the mouse cursor on the item and click "GitZip Download" → "Selected Folder/File".
      3. Click "GitZip Download" → "Checked Items" after doing 2-1-1.
  3. See the progress dashboard and wait for the browser trigger download.
  4. Get the ZIP file.

Get a token:

  1. Click the GitZip Extension icon on your browser.
  2. Click the "Normal" or "Private" link besides "Get Token".
  3. Authorize GitZip permissions on the GitHub authentication page.
  4. Back to the repository page of the beginning.
  5. Continue to use.

Option 2: GitHub / GitHub Pages

http://kinolien.github.io/gitzip by using the GitHub API, and JSZip, FileSaver.js libraries.

Step 1: Input the GitHub URL in the field at the top-right.
Step 2: Press Enter or click Download for downloading the ZIP file directly or click search for viewing the list of subfolders and files.
Step 3: Click the "Download Zip File" or "Get File" button to get the 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 repositories and upgrade the rate limit, if you have a GitHub account and use the "get token" link on this site.

21
  • 1
    @MYounas See about OAuth, Trees, Blobs chapters in Github API Doc., and also use JSZip, FileSaver.js js libraries for gitzip implementation.
    – Kino
    Commented Aug 25, 2016 at 4:02
  • 1
    currently broken ?
    – Ray Hulha
    Commented Dec 14, 2016 at 16:49
  • 1
    I tried to get github.com/vsch/flexmark-java/tree/master/flexmark/src/main/… with current chrome on win 10 and it just hung.
    – Ray Hulha
    Commented Dec 16, 2016 at 15:24
  • 2
    Would anyone b interested in a chrome extension that does this?
    – goldylucks
    Commented Jan 26, 2017 at 10:07
  • 1
    @goldylucks I did this, see the new answer.
    – Kino
    Commented Jun 28, 2017 at 9:30
196

If you have Subversion (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
8
  • Does this still work? Tried this with TortiseSVN and I get a permission error, asking me to supply credentials.
    – john
    Commented Sep 4, 2015 at 14:23
  • 2
    @john I'm sure this is too late to be of much help, but do you have 2-factor authentication enabled on your GitHub account? If so, I think I read somewhere you'll have to use a pre-generated key for the password (which you'll need to find in the GitHub settings).
    – nick
    Commented Oct 23, 2015 at 3:07
  • 1
    Regarding the URL formatting, you can keep the .git extension. You can use the full project link, and start by using svn ls followed by the project full path. Example: svn ls https://github.com/RobTillaart/Arduino.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. It is easier to keep the project path intact.
    – jgrocha
    Commented Feb 21, 2016 at 11:33
  • @jgrocha I'm not sure it worked that way back in 2013, but it does now, so thanks, updated the post
    – janos
    Commented Feb 21, 2016 at 11:44
  • 1
    An example: to download the content in https://github.com/miguelgrinberg/python-socketio/tree/master/examples/wsgi, run svn export https://github.com/miguelgrinberg/python-socketio.git/trunk/examples/wsgi. A directory called wsgi will be created under the current working directory. Only source files, nothing else. No .git, no subversion related files.
    – Rockallite
    Commented Mar 10, 2017 at 7:20
106

After trying all the answers, the best solution for me was:

GitHub's Visual Studio Code-based editor.

Pros:

  1. doesn't require any extra tool like SVN or API tokens.
  2. No limit on size of content
  3. Saves as a directory or file, and not archive.

Instructions

  1. Go to any repository. (example: https://github.com/RespiraWorks/Ventilator/tree/master/software)

  2. Press . or replace .com with .dev in the URL to open the repository in GitHub's internal editor

  3. In the Explorer pane (left side or press Ctrl+Shift+E), right-click on the required file/folder and select Download.

  4. In the Select Folder dialog box, choose the directory on your disk under which you want the selected file/folder to exist.

Note

I tried other solutions like in accepted answer but,

  1. I don't want to install and learn SVN only for this.

  2. Other tools like Download Directory, Refined GitHub, GitZip, DownGit either require API tokens or cannot download large directories.

Other options

  • Visual Studio Code with Remote Repositories extension to open the repository and download the file/folder.
14
  • 3
    This doesn't actually work for directories (anymore?). Microsoft seems to have nerfed this to only 'upload' to directories and only download files.
    – i30817
    Commented Feb 16, 2022 at 15:30
  • 14
    @i30817 I found the reason. On checking code, it appears that this feature uses an API(developer.mozilla.org/en-US/docs/Web/API/Window/…) which is only available in chrome/edge/chromium 86+ and opera. If you're using something else, you may not be able to download the folders. Commented Feb 17, 2022 at 20:17
  • 3
    That figures, i was using firefox. Oh well, i no longer need it since my specific case had another solution.
    – i30817
    Commented Feb 17, 2022 at 22:32
  • 3
    It still works as of Mar 7, 2022. I like this approach because when you put a link to another server that makes you a zip file that server has the potential to append malicious stuff. Commented Mar 7, 2022 at 9:57
  • 3
    Works like charm in Aug 2022
    – Tonnie
    Commented Aug 16, 2022 at 9:58
104

For a generic Git repository:

If you want to download files, not clone the repository with history, you can do this with git-archive.

git-archive makes a compressed ZIP or tar archive of a Git repository. Some things that make it special:

  1. You can choose which files or directories in the Git repository to archive.
  2. It doesn't archive the .git/ folder, or any untracked files in the repository it's run on.
  3. You can archive a specific branch, tag, or commit. Projects managed with Git often use this to generate archives of versions of the project (beta, release, 2.0, etc.) for users to download.

An example of creating an archive of the docs/usage directory from a remote repository you're connected to with ssh:

# In a terminal
git archive --format tar --remote ssh://server.org/path/to/Git HEAD docs/usage > /tmp/usage_docs.tar

More information in this blog post and the Git documentation.

Note on GitHub repositories:

GitHub doesn't allow git-archive access. ☹️

5
  • 2
    git archive --format tar format is not tar.gz, but tar. Commented Apr 17, 2018 at 6:10
  • This should be the best answer Commented Aug 3, 2018 at 8:18
  • 9
    I think this is the best answer but keep note that git archive command doesn't work with https protocol.
    – bitfox
    Commented Mar 8, 2019 at 13:00
  • @yukihane edited extension from tar.gz to tar, hopefully OP is onboard with this
    – mihai
    Commented Aug 22, 2019 at 12:35
  • It is possible to do | tar -x to skip the creation of a tar file.
    – Guillermo
    Commented Aug 16, 2022 at 14:36
50

git clone --filter downloads only the required folders

E.g., to clone only objects required for subdirectory small/ of this repository: https://github.com/cirosantilli/test-git-partial-clone-big-small notably ignoring subdirectory big/ which contains large files, I can do:

git clone -n --depth=1 --filter=tree:0 \
  https://github.com/cirosantilli/test-git-partial-clone-big-small
cd test-git-partial-clone-big-small
git sparse-checkout set --no-cone small
git checkout

The --filter option was added together with an update to the remote protocol, and it truly prevents objects from being downloaded from the server.

I have covered this in more detail at: How do I clone a subdirectory only of a Git repository?

Tested on git 2.30.0 on January 2021.

7
  • @VonC I think I beat you to it this once: stackoverflow.com/questions/2466735/… and I only looked at it after the release XD! I was of course Googling today a bit after 2.22 release, all answer have already been updated of course ;-) It never ceases to amuse me how there is always another semi duplicate left, always, and I end up finding it one year later with a slightly different query. I don't intend to beat you on git though, too hard, but keep an eye on necromancer counts! haha Commented Jun 8, 2019 at 11:17
  • @VonC joking of course, I upvote your stuff as well obviously. I just need to find a way to redeem those points into money now XD Commented Jun 8, 2019 at 11:26
  • 1
    I tried getting one folder of this repo, each one contains 1000+ files. github.com/yannh/kubernetes-json-schema/tree/master/…. When I simply clone the entire repo, it is much quicker than getting one folder with the describe method here.
    – The Fool
    Commented Jan 31, 2023 at 23:06
  • 1
    @TheFool OK I reproduced, thanks. This answer contained an outdated command git checkout, my main answer at: stackoverflow.com/questions/600079/… had the correct one git sparse-checkout set. With that it's instantaneous. Updated. Commented Feb 1, 2023 at 8:19
  • 1
    Amazing! This works without using any other website or using svn which github ended support for. But this method seems kinda intuitive and well I learned something new today. Thanks! Commented Feb 24 at 2:32
35

There is nothing wrong with other answers, but I just thought I'd share step-by-step instructions for those wandering through this process for the first time.

How to download a single folder from a GitHub repository (Mac OS X):

~ To open Terminal just click spotlight and type terminal then hit Enter

  1. On a Mac, you likely already have SVN (to test just open terminal and type "svn" or "which svn" ~ without the quote marks)
  2. On GitHub: Locate the GitHub path to your Git folder (not the repository) by clicking the specific folder name within a repository
  3. Copy the path from the address bar of the browser
  4. Open Terminal and type: svn export
  5. Next paste in the address (e.g.): https://github.com/mingsai/Sample-Code/tree/master/HeadsUpUI
  6. Replace the words: tree/master
  7. with the word: trunk
  8. Type in the destination folder for the files (in this example, I store the target folder inside of the Downloads folder for the current user)
  9. Here space is just the spacebar, not the word (space) ~/Downloads/HeadsUpUI
  10. The final terminal command shows the full command to download the folder (compare the address to step 5) svn export https://github.com/mingsai/Sample-Code/trunk/HeadsUpUI ~/Downloads/HeadsUpUI

BTW - If you are on Windows or some other platform, you can find a binary download of Subversion (SVN) at http://subversion.apache.org

~ If you want to checkout the folder rather than simply download it try using the SVN help (tldr: replace export with checkout)

Regarding the comment on resuming an interrupted download/checkout. I would try running svn cleanup followed by svn update. Please search Stack Overflow for additional options.

3
  • 1
    I have to download many files from a specific folder and got this error in the middle of the download: svn: E175002: REPORT of '/bgmartins/scripts-parlamento/!svn/vcc/default': 200 OK (github.com) Do you how can I resume the download?
    – Miguel
    Commented Jun 7, 2017 at 21:56
  • Worked for me. That's amazing. Commented Mar 21, 2019 at 19:38
  • Amazing! Worked great! Commented Mar 25, 2021 at 11:57
33

Whoever is working on a specific folder, he/she needs to clone that particular folder itself. To do so, please follow the below steps by using a sparse checkout.

  1. Create a directory.

  2. Initialize a Git repository (git init)

  3. Enable sparse checkouts. (git config core.sparsecheckout true)

  4. Tell Git which directories you want (echo 2015/brand/May(refer to folder you want to work on) >> .git/info/sparse-checkout)

  5. Add the remote (git remote add -f origin https://jafartke.com/mkt-imdev/DVM.git)

  6. Fetch the files (git pull origin master )

5
  • Too bad this comment did not get enough upvotes. Looks like the solution provided applies to all git repositories and not just the github one.
    – Kalyan
    Commented Aug 26, 2016 at 16:00
  • 12
    This doesn't actually do what was desired, though. It still clones the whole repository, it just doesn't check out some folders.
    – Chris
    Commented Jan 5, 2017 at 4:18
  • Resolved here : stackoverflow.com/questions/4114887/…
    – J K
    Commented Nov 17, 2017 at 16:27
  • I get this error: Sparse checkout leaves no entry on working directory. Commented Apr 5, 2020 at 20:53
  • What is "2015/brand/May"? Commented May 14 at 19:44
27

Just add ss to the start of the GitHub URL: (github.com -> ssgithub.com)

I built this simple webpage that does this for you, so just:

  1. Navigate to the directory/file you want to download on GitHub
  2. Add ss to the start of the URL in the address bar

Clicking on Download should zip just the contents of that directory and download them to your device.

[Demo video]

3
  • 2
    Cool! No webapps or magic needed!
    – Csaba Toth
    Commented Mar 20 at 1:17
  • 1
    Simple, easy, worked like a charm! Commented May 22 at 13:46
  • Didn't work with Firefox lately, is there a Github repo where I can file a ticket?
    – Csaba Toth
    Commented Aug 21 at 22:37
25

You cannot; unlike Subversion (SVN), 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.

It is conceivable that a Git front-end (e.g., GitHub's web interface, or GitWeb) could choose to provide an interface for you to extract a given folder, but to my knowledge none of them do that (though they do let you download individual files, so if the folder does not contain too many files, that is an option)

GitHub actually offers access via SVN, which would allow you to do just this (as per comment). See Improved SVN is here to stay, and old SVN is going away for latest instructions on how to do this.

1
  • is there any thing via github API?
    – g_inherit
    Commented Aug 18, 2011 at 10:47
23

2019 Summary

There are a variety of ways to handle this, depending on whether or not you want to do this manually or programmatically.

There are four options summarized below. And for those that prefer a more hands-on explanation, I've put together a YouTube video: Download Individual Files and Folders from GitHub.

Also, I've posted a similar answer on Stack Overflow for those that need to download single files from GitHub (as opposed to folders).


1. GitHub User Interface

  • There's a download button on the repository's homepage. Of course, this downloads the entire repository, after which you would need to unzip the download and then manually drag out the specific folder you need.

2. Third-party Tools

  • There are a variety of browser extensions and web application that can handle this, with DownGit being one of them. Simply paste in the GitHub URL to the folder (e.g., https://github.com/babel/babel-eslint/tree/master/lib) and press the Download button.

3. Subversion

  • GitHub does not support git-archive (the Git feature that would allow us to download specific folders). GitHub does however, support a variety of Subversion features, one of which we can use for this purpose. Subversion is a version control system (an alternative to Git). You'll need Subversion installed. Grab the GitHub URL for the folder you want to download. You'll need to modify this URL, though. You want the link to the repository, followed by the word "trunk", and ending with the path to the nested folder. In other words, using the same folder link example that I mentioned above, we would replace "tree/master" with "trunk". Finally, open up a terminal, navigate to the directory that you want the content to get downloaded to, type in the following command (replacing the URL with the URL you constructed): svn export https://github.com/babel/babel-eslint/trunk/lib, and press enter.

4. GitHub API

  • This is the solution you'll need if you want to accomplish this task programmatically. And this is actually what DownGit is using under the hood. Using GitHub's REST API, write a script that does a GET request to the content endpoint. The endpoint can be constructed as follows: https://api.github.com/repos/:owner/:repo/contents/:path. After replacing the placeholders, an example endpoint is: https://api.github.com/repos/babel/babel-eslint/contents/lib. This gives you JSON data for all of the content that exists in that folder. The data has everything you need, including whether or not the content is a folder or file, a download URL if it's a file, and an API endpoint if it's a folder (so that you can get the data for that folder). Using this data, the script can recursively go through all content in the target folder, create folders for nested folders, and download all of the files for each folder. Check out DownGit's code for inspiration.
1
  • I built an easy to use wrapper around the github api. all you have to do is add ss in front of the url while browsing through the repo on github. explained it here - stackoverflow.com/a/77459783/11565176
    – nsrCodes
    Commented Dec 9, 2023 at 6:22
18

If you truly just want to just "download" the folder and not "clone" it (for development), the easiest way to simply get a copy of the most recent version of the repository (and therefore a folder/file within it), without needing to clone the whole repository or even install Git in the first place, is to download a ZIP archive (for any repository, fork, branch, commit, etc.) by going to the desired repository/fork/branch/commit on GitHub (e.g. http(s)://github.com/<user>/<repo>/commit/<Sha1> for a copy of the files as they were after a specific commit) and selecting the Downloads button near the upper-right.

This archive format contains none of the git-repo magic, just the tracked files themselves (and perhaps a few .gitignore files if they were tracked, but you can ignore those :p)—that means that if the code changes and you want to stay on top, you'll have to manually redownload it, and it also means you won't be able to use it as a Git repository...

I am not sure if that's what you're looking for in this case (again, "download"/view vs "clone"/develop), but it can be useful nonetheless...

3
  • 10
    If you don't see a download link, make sure you're on a main repo page (not a child directory.) Seems like you can only download a zip of the entire repo. Also, this github help page might be useful.
    – JD Smith
    Commented Sep 5, 2013 at 16:07
  • 3
    Easy point-and-click access. Easy to get a prior release. Easy to pull the desired folder out of the downloaded zip. Thank you. Commented Apr 9, 2015 at 21:33
  • 2
    The generic URL to get download from in tar.gz: https://github.com/${owner}/${repo}/archive/${hash}.tar.gz
    – Krisztian
    Commented Aug 12, 2017 at 19:04
14

There's a Python3 pip package called githubdl that can do this*:

export GIT_TOKEN=1234567890123456789012345678901234567890123
pip install githubdl
githubdl -u http://github.com/foobar/test -d foo

The project page is here

* Disclaimer: I wrote this package.

2
  • Thank you for sharing this package, I have been helpful in the COLAB Environment Commented Mar 13, 2021 at 22:16
  • You got my $1. Thanks!
    – GTodorov
    Commented Apr 24, 2021 at 16:13
12

If you are comfortable with Unix commands, you don't need special dependencies or web applications for this. You can download the repository as a tarball and untar only what you need.

Example (woff2 files from a subdirectory in Font Awesome):

curl -L https://api.github.com/repos/FortAwesome/Font-Awesome/tarball | tar xz --wildcards "*/web-fonts-with-css/webfonts/*.woff2" --strip-components=3
  • More about the link format: https://developer.github.com/v3/repos/contents/#get-archive-link (including how to get a ZIP file or specific branches/refs)
  • Keep the initial part of the path (*/) to match any directory. GitHub creates a wrapper directory with the commit ref in the name, so it can't be known.
  • You probably want --strip-components to be the same as the amount of slashes (/) in the path (previous argument).

This will download the whole tarball. Use the SVN method mentioned in the other answers if this has to be avoided or if you want to be nice to the GitHub servers.

11

This is how I do it with Git v2.25.0, and it was also tested with v2.26.2. This trick doesn't work with v2.30.1.

TLDR

git clone --no-checkout --filter=tree:0 https://github.com/opencv/opencv
cd opencv

# Requires Git 2.25.x to 2.26.2
git sparse-checkout set data/haarcascades

You can use Docker to avoid installing a specific version of Git:

git clone --no-checkout --filter=tree:0 https://github.com/opencv/opencv
cd opencv

# Requires Git 2.25.x to 2.26.2
docker run --rm -it -v $PWD/:/code/ --workdir=/code/ alpine/git:v2.26.2 sparse-checkout set data/haarcascades

Full solution

# Bare minimum clone of OpenCV
git clone --no-checkout --filter=tree:0 https://github.com/opencv/

Output:

opencv
...
Resolving deltas: 100% (529/529), done.

# Downloaded only ~7.3MB , takes ~3 seconds
# du = disk usage, -s = summary, -h = human-readable

And:

du -sh opencv

Output:

7.3M    opencv/

And:

# Set target directory
cd opencv
git sparse-checkout set data/haarcascades

Output:

...
Updating files: 100% (17/17), done.
# Takes ~10 seconds, depending on your specifications

And:

# View downloaded files
du -sh data/haarcascades/

Output:

9.4M    data/haarcascades/

And:

ls data/haarcascades/

Output:

haarcascade_eye.xml                      haarcascade_frontalface_alt2.xml      haarcascade_licence_plate_rus_16stages.xml  haarcascade_smile.xml
haarcascade_eye_tree_eyeglasses.xml      haarcascade_frontalface_alt_tree.xml  haarcascade_lowerbody.xml                   haarcascade_upperbody.xml
haarcascade_frontalcatface.xml           haarcascade_frontalface_default.xml   haarcascade_profileface.xml
haarcascade_frontalcatface_extended.xml  haarcascade_fullbody.xml              haarcascade_righteye_2splits.xml
haarcascade_frontalface_alt.xml          haarcascade_lefteye_2splits.xml       haarcascade_russian_plate_number.xml

References

2
  • 2
    Why doesn't it work with v2.30.1?
    – Akito
    Commented Aug 1, 2022 at 0:51
  • In my quick test just now on 2.38.1, I just had to do a git checkout main after running git sparse-checkout set foo, and it seemed to only pull down the single folder that I had specified. Commented Nov 9, 2022 at 23:48
10

You can use git-svn in the following way.

First, replace tree/master with trunk. Then, install git-svn by sudo apt install git-svn.

git svn clone https://github.com/lodash/lodash/trunk/test

This way, you don't have to go through the pain of setting SVN, specifically for Windows users.

2
  • Thank You. I can vouch that it works for Windows. Doesn't work on gcloud though. It says git: 'svn' is not a command Commented Mar 16, 2020 at 20:12
  • 4
    sudo apt install git-svnis required when running through WSL.
    – clubby789
    Commented Mar 31, 2020 at 11:05
9

You can do a simple download of the directory tree:

git archive --remote [email protected]:foobar/Test.git HEAD:foo | tar xf -

But if you mean to check it out, and be able to do commits and push them back, no you can't do that.

3
  • 1
    Invalid command: 'git-upload-archive 'foobar/Test.git'' You appear to be using ssh to clone a git:// URL. Make sure your core.gitProxy config option and the GIT_PROXY_COMMAND environment variable are NOT set. fatal: The remote end hung up unexpectedly tar: This does not look like a tar archive tar: Exiting with failure status due to previous errors
    – g_inherit
    Commented Aug 18, 2011 at 11:57
  • @g_inherit I can reproduce this issue. Although this might be the right direction, this answer is invalid. Commented Nov 5, 2014 at 15:31
  • despite so many down votes this is the only advice that worked for me.
    – bhaskarc
    Commented Dec 21, 2014 at 0:05
9

git sparse-checkout

Git 2.25.0 includes a new experimental git sparse-checkout command that makes the existing feature easier to use, along with some important performance benefits for large repositories. (The GitHub Blog)

Example with current version:

git clone --filter=blob:none --sparse https://github.com/git/git.git
cd git
git sparse-checkout init --cone
git sparse-checkout add t

Most notably

  • --sparse checks out only top-level directory files of git repository into working copy
  • git sparse-checkout add t incrementally adds/checks out t subfolder of git

Other elements

  • git sparse-checkout init does some preparations to enable partial checkouts
  • --filter=blob:none optimizes data fetching by downloading only necessary git objects (take a look at partial clone feature for further infos)
  • --cone also speeds up performance by applying more restricted file inclusion patterns

GitHub status

GitHub is still evaluating this feature internally while it’s enabled on a select few repositories [...]. As the feature stabilizes and matures, we’ll keep you updated with its progress. (docs)

1
9

Another specific example:

Like I want to download 'iOS Pro Geo' folder from the URL https://github.com/alokc83/APRESS-Books-Source-Code-/**tree/master**/%20Pro%20iOS%20Geo

and I can do so via

svn checkout https://github.com/alokc83/APRESS-Books-Source-Code-/trunk/%20Pro%20iOS%20Geo

Note trunk in the path.

Yes, using export instead of checkout would give a clean copy without extra Git repository files.

svn export https://github.com/alokc83/APRESS-Books-Source-Code-/trunk/%20Pro%20iOS%20Geo

If tree/master is not there in the URL, then fork it and it will be there in the forked URL.

2
  • 2
    Just need to change checkout to export if you just want to download a copy of the folder rather than checkout.
    – Tommie C.
    Commented Sep 13, 2015 at 0:14
  • Thank you for your answer, can we dive a branch ? let's say I want to download feature1 branch's aaa folder
    – hsafarya
    Commented Nov 10, 2016 at 14:05
7

None of the answers helped in my situation. If you are developing for Windows, you likely don't have svn. In many situations one can't count on users to have Git installed either, or don't want to download entire repositories for other reasons. Some of the people that answered this question, such as Willem van Ketwich and aztack, made tools to accomplish this task. However, if the tool isn't written for the language you are using, or you don't want to install a third party library, these don't work.

However, there is a much easier way. GitHub has an API that allows you to download a single file or an entire directory's contents using GET requests. You can access a directory using https://api.github.com/repos/:owner/:repo_name/contents/:path that returns a JSON object enumerating all the files in the directory. Included in the enumeration is a link to the raw content of the file, the download_url parameter. The file can then be downloaded using that URL.

It's a two step process that requires the ability to make GET requests, but this can be implemented in pretty much any language, on any platform. It can be used to get files or directories.

1
  • 1
    This works only for single files. The OP is asking for a way to download a folder and the download_url for folders is null. Please, read the question carefuly, before posting an answer.
    – scopchanov
    Commented Dec 6, 2020 at 16:55
7

You can use ghget with any URL copied from the address bar:

ghget https://github.com/fivethirtyeight/data/tree/master/airline-safety

It's a self-contained portable shell script that doesn't use SVN (which didn't work for me on a big repo). It also doesn't use the API so it doesn't require a token and isn't rate-limited.

Disclaimer: I made it.

7

For whatever reason, the svn solution does not work for me, and since I have no need of svn for anything else, it did not make sense to spend time trying to make it, so I looked for a simple solution using tools I already had. This script uses only curl and awk to download all files in a GitHub directory described as "/:user:repo/contents/:path".

The returned body of a call to the GitHub REST API "GET /repos/:user:repo/contents/:path" command returns an object that includes a "download_url" link for each file in a directory.

This command-line script calls that REST API using curl and sends the result through AWK, which filters out all but the "download_url" lines, erases quote marks and commas from the links, and then downloads the links using another call to curl.

curl -s https://api.github.com/repos/:user/:repo/contents/:path | awk \
     '/download_url/ { gsub("\"|,", "", $2); system("curl -O " $2); }'
4
  • I get awk: cmd. line:1: /download_url/ { gsub("\"|,", "", $2); system("curl -O "$2"); } awk: cmd. line:1: ^ syntax error
    – Botspot
    Commented Sep 23, 2020 at 2:10
  • Batch equivalent of this command: FOR /F delims^=^"^ tokens^=4 %%a IN ('curl -s https://api.github.com/repos/:user/:repo/contents/:path 2^>NUL ^| findstr "download_url"') DO curl -O "%%~a"
    – shawn
    Commented Mar 22, 2021 at 11:05
  • My favourite solution, thanks to curl and awk. if only it could recurse.
    – Samveen
    Commented Mar 12 at 18:33
  • Another variant using jq: curl -Ls 'https://api.github.com/repos/home-assistant/core/contents/homeassistant/components/aladdin_connect?ref=2024.6.4' | jq -r '.[].download_url' | xargs -n1 curl -O
    – Vynce
    Commented Jul 20 at 19:24
6

Just 5 steps to go

  • Download SVN from here.
  • Open CMD and go to SVN bin directory like: cd %ProgramFiles%\SlikSvn\bin
  • Let's suppose I wan to download this directory URL
    https://github.com/ZeBobo5/Vlc.DotNet/tree/develop/src/Samples
  • Replace tree/develop or tree/master with trunk
  • Now fire this last command to download folder in same directory.
svn export https://github.com/ZeBobo5/Vlc.DotNet/trunk/src/Samples
0
6

Just to amplify previous answers, a real example from a real GitHub repository to a local directory would be:

svn ls https://github.com/rdcarp/playing-cards/trunk/PumpkinSoup.PlayingCards.Interfaces

svn export https://github.com/rdcarp/playing-cards/trunk/PumpkinSoup.PlayingCards.Interfaces  /temp/SvnExport/Washburn

Sometimes a concrete example helps clarify the substitutions proposed.

5

It's one of the few places where SVN is better than Git.

In the end we've gravitated towards three options:

  1. Use wget to grab the data from GitHub (using the raw file view).
  2. Have upstream projects publish the required data subset as build artifacts.
  3. Give up and use the full checkout. It's big hit on the first build, but unless you get lot of traffic, it's not too much hassle in the following builds.
1
  • 5
    Well, it's less Git vs Subversion and more that GitHub flat-out doesn't allow git-archive. (I'd love to know why.)
    – Rob Howard
    Commented Sep 27, 2013 at 11:24
5

I use Linux so, put this in ~/.bashrc, called even :D $HOME/.bashrc

git-dowloadfolder(){
a="$1"
svn checkout ${a/tree\/master/trunk}

}

Then refresh the shell with:

source ~/.bashrc

Then use it with git-downloadfolder blablabla :D

5

I have developed a tool that might be exactly what you need:

  1. Visit: https://techhelpbd.com/gitdown

  2. Paste your GitHub folder link

  3. Then you can easily download your GitHub folder

Give it a try and let me know how it works for you

4

I have created a simple application that supports download directories, files, and repositories (Private/Public).

Application: https://downdir.vercel.app/

GitHub: https://github.com/renomureza/downdir

4

To export a directory from GitHub, replace "/tree/master/" in the directory's URL with "/trunk/".

For example, to export the directory from the following URL:

https://github.com/liferay/liferay-plugins/tree/master/portlets/sample-hibernate-portlet

run the following command:

svn export https://github.com/liferay/liferay-plugins/trunk/portlets/sample-hibernate-portlet

Not the answer you're looking for? Browse other questions tagged or ask your own question.