1875

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:

git@github.com: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.

Bitswazsky
  • 4,242
  • 3
  • 29
  • 58
g_inherit
  • 18,771
  • 3
  • 16
  • 5
  • 13
    I wrote a shell script for this using svn checkout - https://github.com/Decad/github-downloader – Declan Cook Nov 05 '13 at 11:01
  • 798
    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 Jul 14 '14 at 05:02
  • 4
    see @janos answer svn alternative is the best, no auth, no registration, no limit, no plugins – stackdave Oct 11 '17 at 10:30
  • 3
    I've created an open source project, called [GitHubFolderDownloader](https://github.com/VahidN/GitHubFolderDownloader). It lets you to download a single folder of a repository without cloning or downloading the whole repository. – VahidN Jun 17 '15 at 10:16
  • 6
    @Alexander There is an official way. Check my answer https://stackoverflow.com/a/70729494/11218031 – Avinash Thakur Jan 22 '22 at 06:25
  • +1 for Declan's shell script - it lets you specify just the repo path and you're off to the races. In case people want to hack things further in python, here's a python version of Declan's svn script, updated to work with branches named `main` as well as `master`: [github_downloader.py](https://github.com/dp12/dotfiles/blob/master/python-scripts/github_downloader.py) – nandkeypull Apr 15 '22 at 16:33
  • 1
    There's an official solution now in git using sparse clone (assuming cloning instead of downloading works for you too). Check the 2022 answer here that works perfectly! > https://stackoverflow.com/questions/600079/how-do-i-clone-a-subdirectory-only-of-a-git-repository/73254328#73254328 – Evan MJ Aug 05 '22 at 19:15

43 Answers43

1708

Update Apr. 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 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.

Minhas Kamal
  • 20,752
  • 7
  • 62
  • 64
nick
  • 18,784
  • 2
  • 24
  • 31
  • 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 Dec 18 '13 at 18:34
  • Would there be a similar solution if the folder is in a branch? – num3ric Oct 09 '14 at 04:20
  • 8
    @num3ric- sorry for the late reply, but you should be able to replace "trunk" with "branches/yourbranch" – nick Oct 25 '14 at 21:49
  • Thanks for your answer. With TortoiseSVN, I can easily and visually operate everything without caring about the ```master``` & ```trunk``` problem. – yushulx Apr 16 '15 at 06:36
  • Does this still work? Tried this with TortiseSVN and I get a permission error, asking me to supply credentials. – john Sep 04 '15 at 14:23
  • @john Are you sure you are using the correct URL? You shouldn't have to provide credentials to get a "read-only" copy of the repo. – nick Sep 04 '15 at 16:07
  • 2
    I'm using TortoiseSVN's `Import` command. I am sure I'm providing the correct URL in a similar format as shown in the answer. I even tried using the visual inspector and selected the required folder (no URL typed) and the result is the same. – john Sep 05 '15 at 07:15
  • I tried it with Jekyll and get "This repository is hosted on a new server that doesn't support SVN yet. Please contact support if you need SVN access for this repository." – Thomas Weller Sep 20 '15 at 20:33
  • Why does this work ? Does github explicitly support svn somehow? – James Wierzba Oct 08 '15 at 19:38
  • @JamesWierzba yes, GitHub added `svn` support a few years ago. It used to be read-only (you could only do checkouts against a github repo) but they've since added support for a lot of other things, like commits. I've added a couple links to my answer which will probably explain things a lot better. – nick Oct 08 '15 at 21:44
  • If you are using Github 2 factor auth you will need to generate an access code which you can use as a password on the CLI => https://help.github.com/articles/creating-an-access-token-for-command-line-use/ – Elijah Lynn Oct 20 '15 at 00:58
  • @technophyle it still does. I think this post is due for an update though so I'll try to make the steps more clear. – nick Aug 22 '16 at 05:04
  • @nick fixed 'branch' to 'branches' in the Full URL format explanation, second bullet. The copy/paste didn't work, so I noticed it :) – yair Dec 27 '16 at 23:40
  • nice. additional example for folks wanting all images "provided" by cv2: svn checkout 'https'://github.com/opencv/opencv/trunk/samples/data – Quetzalcoatl Nov 30 '18 at 07:36
  • For those without `svn` installed, I used `git svn clone` in place of `svn checkout` and all went well (it does add a .git directory which is easy enough to delete if necessary). – SpencerPark May 28 '19 at 20:38
  • Thanks this works. It is a shame that github does not provide a simple download button in the web interface. You can only download the entire repo as a zip file... – Mike76 Sep 25 '19 at 10:01
  • `svn checkout https://github.com/lodash/lodash/trunk/test` doesn't work in Colab – dereks Jan 02 '20 at 13:43
  • and once you have it in local are you able to push changes in the folder? – David Marciel Mar 18 '20 at 10:41
  • @DavidMarciel I believe so, but you would need to commit/push changes with svn, cannot use git. read more here: https://help.github.com/en/github/importing-your-projects-to-github/support-for-subversion-clients – nick Mar 19 '20 at 02:33
  • what if it's a private repo? – Freedo Apr 06 '20 at 09:45
  • @SérgioMichels Is there a way to download the folder in compressed format? – pooya13 Jun 05 '20 at 17:45
  • 4
    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` – evaldeslacasa Jun 18 '20 at 11:11
  • 1
    DownGit is plug n play – Moises Rodan Jul 30 '20 at 05:05
  • 8
    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 Oct 28 '20 at 14:59
  • Is there a way to use svn checkout for git repository using ssh ?? – avinashjha Jan 14 '21 at 09:00
  • `svn export https://github.com/$organization/$repo/branches/$branch/$directory` worked fine with `svn --version` `1.10.3 (r1842928)` and [URL](https://github.com/docker-library/php/branches/master/7.4/bullseye/cli) now – vike Jan 14 '22 at 08:27
  • 2
    @Hugo there's a better way which doesn't need any extra tool. Posted it in this answer https://stackoverflow.com/a/70729494/11218031 – Avinash Thakur Jan 16 '22 at 11:17
  • https://download-directory.github.io/ worked best for my case – sultanmyrza Jan 25 '22 at 13:16
  • One may also download specific tags inthe same manner as branches, by replacing branches with tags, e.g. svn checkout https://github.com/ex/ex/tags/foo/pathtofolder – Emma Jane Bonestell Feb 11 '22 at 23:39
  • works also with tags `svn checkout https://github.com///tags//folder` – liorko Jun 12 '22 at 07:07
  • This method does not work with git lfs. – Jérôme Tremblay Nov 10 '22 at 16:33
  • GitHub is ending support of SVN on **January 8, 2024** https://github.blog/2023-01-20-sunsetting-subversion-support/ – Hugo Jan 27 '23 at 20:13
1103

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

Minhas Kamal
  • 20,752
  • 7
  • 62
  • 64
  • 27
    The base method is- 1. take the directory link (used [angular-route](https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular-route.min.js)), 2. go to [GitHub API](https://api.github.com), 3. search through the directory and download all files, 4. create a zip file with it (I have used [jszip](https://cdn.rawgit.com,/Stuk/jszip/v3.0.0/dist/jszip.min.js)) & save it as file (used [filesaver](https://fastcdn.org/FileSaver.js/1.1.20151003/FileSaver.min.js) for this). For clear understanding I would suggest to take a look in [my project](https://github.com/MinhasKamal/DownGit). – Minhas Kamal Aug 20 '16 at 10:06
  • @MYounas Sorry I cannot suggest you any tutorial as I did not follow any. You may get some help from [here](https://developer.github.com/v3/). – Minhas Kamal Aug 20 '16 at 12:51
  • 11
    This is cool but it doesn't work with automated tools (like wget) – Ring Jan 25 '18 at 21:10
  • Thanks for creating this, but it does not work for me: Trying to download a directory from MSDN code samples. Git url is https://github.com/microsoftarchive/msdn-code-gallery-microsoft.git Directory is: msdn-code-gallery-microsoft/Official Windows Platform Sample/XAML SwapChainPanel DirectX interop sample/ URL from downgit is: https://github.com/microsoftarchive/msdn-code-gallery-microsoft/Official Windows Platform Sample/XAML SwapChainPanel DirectX interop sample/ I then tried wget "above URL&fileName=swapPanel.zip&rootDirectory=true" just created index.html in directory, no download – Paulus Oct 13 '20 at 09:28
  • Also tried download directly on downgit webpage, but got a 404 error – Paulus Oct 13 '20 at 09:28
  • @Paulus https://github.com/microsoftarchive/msdn-code-gallery-microsoft/Official is not a valid URL. Please check. – Minhas Kamal Oct 13 '20 at 17:35
  • Hi Minas, I just copied the link address of the first URL in my comment, pasted it into chrome, and came out at the github page. Should I maybe leave off the .git suffix? maybe spaces in Microsoft's directory names are causing a problem? – Paulus Oct 14 '20 at 12:19
  • @Paulus I see the repo is too big, it far surpasses 500 file limit. You can download a sub-folder though: [link](https://minhaskamal.github.io/DownGit/#/home?url=https:%2F%2Fgithub.com%2Fmicrosoftarchive%2Fmsdn-code-gallery-microsoft%2Ftree%2Fmaster%2FOfficial%20Windows%20Driver%20Kit%20Sample%2FWindows%20Driver%20Kit%20(WDK)%208.1%20Samples%2F%5BC%2B%2B%5D-windows-driver-kit-81-cpp%2FWDK%208.1%20C%2B%2B%20Samples%2FAC97%20Driver%20Sample%2FC%2B%2B%2FINFViewer%2FHTML) – Minhas Kamal Oct 14 '20 at 12:55
  • Not working on link https://github.com/Apress/pro-asp.net-core-3/tree/master/03%20-%20Your%20First%20Application/PartyInvites – Woland Jan 12 '22 at 02:10
  • @Woland I was able to successfully download the directory you shared. Please check if you have any script blocker or access right issues. – Minhas Kamal Jan 12 '22 at 02:56
  • before doing this make sure your repository is public , if it is private then goto settings and first make it public repository. – Ajay jangid Jan 29 '22 at 03:20
  • 3
    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. – James Hubert May 10 '22 at 05:09
  • Not work for https://github.com/dotnet/aspnetcore/tree/release/6.0/src/SignalR – zwcloud Jul 04 '22 at 13:37
  • @zwcloud this is too big a directory, so GitHub (and not DownGit) does not allow to download it. – Minhas Kamal Jul 11 '22 at 09:13
  • 1
    Thank God we don't need Leonardo da Vinci to download a folder from Github – unstuck Nov 23 '22 at 10:40
  • 2
    this is simple and helpful (- : – jsBug Dec 20 '22 at 06:08
  • I got stuck at 219 files when downloading sample code from asp.net core. Download Directory did it for me 1.6k files – remondo Jan 26 '23 at 08:40
  • you are a hero my friend – smoothumut Mar 15 '23 at 06:26
  • This is my preferred solution as it doesn't requiring downloading or using additional software. – plu Jul 31 '23 at 18:00
  • The website got blacklisted by my antivirus... – Vasco Cansado Carvalho Aug 19 '23 at 20:34
686

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 download button at the bottom-right of the page.
    2. In 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 browser trigger download.
  4. Get the ZIP file.

Get Token:

  1. Click GitZip Extension icon on your browser.
  2. Click "Normal" or "Private" link besides "Get Token".
  3. Authorize GitZip permission on Github auth page.
  4. Back to repo page of the beginning.
  5. Continue to use.

Option 2: Github gh-page

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

Step1: Input github url to the field at the top-right.
Step2: Press enter or click download for download zip directly or click search for view the list of sub-folders and files.
Step3: Click "Download Zip File" or "Get File" button to get 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 repos and upgrade the rate limit, if you have GitHub account and use "get token" link in this site.

Kino
  • 7,193
  • 1
  • 13
  • 14
  • @MYounas How you want to use it? Do you mean create a download link with any repo directory path, like "http://kinolien.github.io/gitzip/?repo=download/repo/path" ? – Kino Aug 21 '16 at 08:08
  • Nop,man i want any helpfull link through which i am able to learn how i can make this type of service like you did "gitzip". – Muhammad Younus Aug 24 '16 at 14:51
  • 1
    @MYounas See about OAuth, Trees, Blobs chapters in [Github API Doc.](https://developer.github.com/v3/), and also use JSZip, FileSaver.js js libraries for gitzip implementation. – Kino Aug 25 '16 at 04:02
  • 1
    currently broken ? – Ray Hulha Dec 14 '16 at 16:49
  • @RayHulha Hi, What's it shows? What's your browser? safari, chrome or others? Maybe you can make a new issue on [github repo](https://github.com/KinoLien/gitzip/issues). – Kino Dec 16 '16 at 00:18
  • 1
    I tried to get https://github.com/vsch/flexmark-java/tree/master/flexmark/src/main/java with current chrome on win 10 and it just hung. – Ray Hulha Dec 16 '16 at 15:24
  • 2
    Would anyone b interested in a chrome extension that does this? – Shining Love Star Jan 26 '17 at 10:07
  • I don't know if I understand the GitHub authorization request in your service, but as I interpret it, I would be granting you a read and write access to my account. Is that correct? – mr5 Jun 21 '17 at 05:07
  • @mr5 Basically yes, but this point was a dilemma to me, and also to others (refers: [jollygoodcode issues](https://github.com/jollygoodcode/jollygoodcode.github.io/issues/6)). We cannot get a read-only permission for your repositories. – Kino Jun 21 '17 at 05:59
  • 1
    @goldylucks I did this, see the new answer. – Kino Jun 28 '17 at 09:30
  • @stackdave Sorry, i don't get it. Do you mean it is not private in the process of get a token? – Kino Oct 11 '17 at 08:45
  • best for me was svn, no auth needed, these programs need Auth – stackdave Oct 11 '17 at 10:28
  • No Firefox version? – Aaron Franke Jan 14 '18 at 19:39
  • 1
    @AaronFranke Now available, try it! – Kino Jan 22 '18 at 06:32
  • 1
    @xamDev Could you tell me the url? – Kino Apr 01 '18 at 08:12
  • @xamDev I tried but it works well. (environment: Chrome: 65.0.3325.162, gitzip: 0.2.1) And have you try in the gh-page? also empty zip? – Kino Apr 02 '18 at 04:09
  • definitely not working; as of this post's date, the spin symbol in the download button gives the impression to be doing something, but nothing actually happens :-( – Orco Jun 11 '18 at 22:56
  • @Kino Is there a way to download fx. https://kinolien.github.io/gitzip/?download=KinoLien/gitzip without using a browser? Fx curl or python? – Paw Feb 18 '19 at 22:38
  • 1
    @Paw I don't have the plan currently. Maybe you can use SVN or [Willem's answer](https://stackoverflow.com/a/50925766/2238770) instead. – Kino Feb 19 '19 at 03:38
  • 1
    Instruction (Usage #2) apparently (at least in Chrome?) should say "right click" not double click, on a blank space on the repo page of the items you need. Then step 3 is just to click on the "download as zip" option that comes up in the right click menu. Once I figured out for myself how to use the extension this was a great solution. – Josh Sutterfield Jan 03 '20 at 22:37
  • 1
    @JoshSutterfield I installed the extension on chrome but right click on blank space of github not showing download as zip option. Is your extension still working? – Louis Go Sep 14 '20 at 12:59
196

If you have 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
janos
  • 120,954
  • 29
  • 226
  • 236
  • Does this still work? Tried this with TortiseSVN and I get a permission error, asking me to supply credentials. – john Sep 04 '15 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 Oct 23 '15 at 03: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 Feb 21 '16 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 Feb 21 '16 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 Mar 10 '17 at 07:20
  • Best alternative for me – Wilder Pereira Nov 11 '17 at 20:17
  • `git svn clone https://github.com/Qamar4P/LolAdapter.git/trunk/lol-adapter -r HEAD` Nice alternative ref https://stackoverflow.com/a/37735157/5710872 – Qamar Aug 07 '19 at 05:42
97

For a Generic git Repo:

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 repo you're connected to with ssh:

# in 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 Repos:

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

mihai
  • 37,072
  • 9
  • 60
  • 86
RobW
  • 10,184
  • 4
  • 41
  • 40
73

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

GitHub's vscode 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 repo. (ex. https://github.com/RespiraWorks/Ventilator/tree/master/software)
  2. Press . or replace .com with .dev in URL to open the repo in GitHub's internal editor
  3. In 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. 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

Avinash Thakur
  • 1,640
  • 7
  • 16
  • 2
    This doesn't actually work for directories (anymore?). Microsoft seems to have nerfed this to only 'upload' to directories and only download files. – i30817 Feb 16 '22 at 15:30
  • @i30817 Verified just now that it works. When you download a folder(A), it asks you to choose a local folder(B). Then, it creates A under B while showing the progress at the status bar of code editor. Is anything different for you ? – Avinash Thakur Feb 17 '22 at 11:44
  • Yes. If you select a folder, only 'upload' appears. To select multiple files and download is possible, though, you'll have multiple downloads appear, one per file. – i30817 Feb 17 '22 at 13:10
  • 10
    @i30817 I found the reason. On checking code, it appears that this feature uses an API(https://developer.mozilla.org/en-US/docs/Web/API/Window/showDirectoryPicker) 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. – Avinash Thakur Feb 17 '22 at 20:17
  • 2
    That figures, i was using firefox. Oh well, i no longer need it since my specific case had another solution. – i30817 Feb 17 '22 at 22:32
  • 1
    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. – sultanmyrza Mar 07 '22 at 09:57
  • 2
    Works like charm in Aug 2022 – Tonnie Aug 16 '22 at 09:58
  • @AvinashThakur, **What browser requirements?** tho – Pacerier Apr 24 '23 at 21:47
  • @Pacerier only Chrome or Chromium based(Edge, Opera, etc.). not firefox, safari, etc. – Avinash Thakur Apr 27 '23 at 13:52
36

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 repo) by clicking the specific folder name within a repo
  3. Copy the path from the address bar of the browser
  4. Open Terminal and type: svn export
  5. Next paste in the address (eg.): 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)

Update

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

Tommie C.
  • 12,895
  • 5
  • 82
  • 100
29

Whoever is working on specific folder he needs to clone that particular folder itself, to do so please follow below steps by using 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 )

iBug
  • 35,554
  • 7
  • 89
  • 134
Mohammed Jafar
  • 493
  • 6
  • 4
  • 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 Aug 26 '16 at 16:00
  • 11
    This doesn't actually do what was desired, though. It still clones the whole repository, it just doesn't check out some folders. – Chris Jan 05 '17 at 04:18
  • Resolved here : https://stackoverflow.com/questions/4114887/is-it-possible-to-do-a-sparse-checkout-without-checking-out-the-whole-repository – J K Nov 17 '17 at 16:27
  • I get this error: Sparse checkout leaves no entry on working directory. – orangecaterpillar Apr 05 '20 at 20:53
27

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.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
  • @VonC I think I beat you to it this once: https://stackoverflow.com/questions/2466735/how-to-sparsely-checkout-only-one-single-file-from-a-git-repository/52270527#comment91530496_52270527 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 – Ciro Santilli OurBigBook.com Jun 08 '19 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 – Ciro Santilli OurBigBook.com Jun 08 '19 at 11:26
  • this takes ages. It fetches each file in d1, individually. – The Fool Jan 31 '23 at 22:39
  • @TheFool I get similar times in both on my test repos, let me know if you have a precise test repo + commands. – Ciro Santilli OurBigBook.com Jan 31 '23 at 22:58
  • I tried getting one folder of this repo, each one contains 1000+ files. https://github.com/yannh/kubernetes-json-schema/tree/master/master-standalone-strict. When I simply clone the entire repo, it is much quicker than getting one folder with the describe method here. – The Fool Jan 31 '23 at 23:06
  • 1
    @TheFool OK I reproduced, thanks. This answer contained an outdated command `git checkout`, my main answer at: https://stackoverflow.com/questions/600079/how-do-i-clone-a-subdirectory-only-of-a-git-repository/52269934#52269934 had the correct one `git sparse-checkout set`. With that it's instantaneous. Updated. – Ciro Santilli OurBigBook.com Feb 01 '23 at 08:19
25

You cannot; 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.

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)

Edit - GitHub actually offers access via SVN, which would allow you to do just this (as per comment). See https://github.com/blog/1438-improved-svn-here-to-stay-old-svn-going-away for latest instructions on how to do this

michel-slm
  • 9,438
  • 3
  • 32
  • 31
22

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 StackOverflow 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 repo, 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 apps 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.
jabacchetta
  • 45,013
  • 9
  • 63
  • 75
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 repo or even install git in the first place, is to download a zip archive (for any repo, 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 re-download it, and it also means you won't be able to use it as a git repository...

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...

johnny
  • 4,024
  • 2
  • 24
  • 38
  • 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](https://help.github.com/articles/downloading-files-from-the-command-line) might be useful. – JD Smith Sep 05 '13 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. – Andy Thomas Apr 09 '15 at 21:33
  • 2
    The generic URL to get download from in `tar.gz`: `https://github.com/${owner}/${repo}/archive/${hash}.tar.gz` – Krisztian Aug 12 '17 at 19:04
13

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.

Willem van Ketwich
  • 5,666
  • 7
  • 49
  • 57
11

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

Example (woff2 files from a subdirectory in fontawesome):

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.

Albin
  • 2,410
  • 2
  • 29
  • 27
11

This is how I do it with git v2.25.0, 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/opencv
...
Resolving deltas: 100% (529/529), done.

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

# Set target dir
$ cd opencv
$ git sparse-checkout set data/haarcascades
...
Updating files: 100% (17/17), done.
# Takes ~10 seconds, depending on your specs

# View downloaded files
$ du -sh data/haarcascades/
9.4M    data/haarcascades/
$ ls data/haarcascades/
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

Meir Gabay
  • 2,870
  • 1
  • 24
  • 34
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

Edited: (as per Tommie C's comment)

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

Edited: If tree/master is not there in url then Fork it and it will be there in Forked url.

zeeawan
  • 6,667
  • 2
  • 50
  • 56
  • 2
    Just need to change checkout to export if you just want to download a copy of the folder rather than checkout. – Tommie C. Sep 13 '15 at 00:14
  • Thank you for your answer, can we dive a branch ? let's say I want to download feature1 branch's aaa folder – hsafarya Nov 10 '16 at 14:05
9

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.

kiranr
  • 2,087
  • 14
  • 32
V3K3R
  • 164
  • 1
  • 5
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)

ford04
  • 66,267
  • 20
  • 199
  • 171
9

You can do a simple download of the directory tree:

git archive --remote git@github.com: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.

araqnid
  • 127,052
  • 24
  • 157
  • 134
  • 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 Aug 18 '11 at 11:57
  • @g_inherit I can reproduce this issue. Although this might be the right direction, this answer is invalid. – Uli Köhler Nov 05 '14 at 15:31
  • despite so many down votes this is the only advice that worked for me. – bhaskarc Dec 21 '14 at 00: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.

Calvin
  • 179
  • 1
  • 7
  • 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 Dec 06 '20 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.

mohd.akram
  • 121
  • 2
  • 3
6

Just to amplify the answers above, 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.

John Washburn
  • 568
  • 1
  • 5
  • 11
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
Gray Programmerz
  • 479
  • 1
  • 5
  • 22
Sorry IwontTell
  • 466
  • 10
  • 29
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.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Manish Shrivastava
  • 30,617
  • 13
  • 97
  • 101
  • 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 Sep 27 '13 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

user1088530
  • 2,152
  • 1
  • 13
  • 14
5

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"); }'
Pinecone
  • 391
  • 4
  • 9
  • I get `awk: cmd. line:1: /download_url/ { gsub("\"|,", "", $2); system("curl -O "$2"); } awk: cmd. line:1: ^ syntax error` – Botspot Sep 23 '20 at 02: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 Mar 22 '21 at 11:05
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
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Mohsen Abasi
  • 2,050
  • 28
  • 30
3

Our team wrote a bash script to do this because we didn't want to have to install SVN on our bare bones server.

https://github.com/ojbc/docker/blob/master/java8-karaf3/files/git-download.sh

It uses the github API and can be run from the command line like this:

git-download.sh https://api.github.com/repos/ojbc/main/contents/shared/ojb-certs
Yogesh Chawla
  • 1,583
  • 18
  • 16
3

I work with CentOS 7 servers on which I don't have root access, nor git, svn, etc (nor want to!) so made a python script to download any github folder: https://github.com/andrrrl/github-folder-downloader

Usage is simple, just copy the relevant part from a github project, let's say the project is https://github.com/MaxCDN/php-maxcdn/, and you want a folder where some source files are only, then you need to do something like:

$ python gdownload.py "/MaxCDN/php-maxcdn/tree/master/src" /my/target/dir/
(will create target folder if doesn't exist)

It requires lxml library, can be installed with easy_install lxml
If you don't have root access (like me) you can create a .pydistutils.py file into your $HOME dir with these contents: [install] user=1 And easy_install lxml will just work (ref: https://stackoverflow.com/a/33464597/591257).

Community
  • 1
  • 1
aesede
  • 5,541
  • 2
  • 35
  • 33
3

Open repo to codesandbox by replacing github to githubbox in url and on codesandbox go to file menu and Export it as a zip.

For following repo: https://github.com/geist-org/react/tree/master/examples/custom-themes

Enter following url: https://githubbox.com/geist-org/react/tree/master/examples/custom-themes

In codesandbox go to file menu and Export it as a Zip.

3

try it.

https://github.com/twfb/git-directory-download

usage: gitd [-h] [-u URL] [-r] [-p] [--proxy PROXY]

optional arguments:
  -h, --help         show this help message and exit
  -u URL, --url URL  github url, split by ",", example: "https://x, http://y"
  -r, --raw          download from raw url
  -p, --parse        download by parsing html
  --proxy PROXY      proxy config, example "socks5://127.0.0.1:7891"

Example:
  1. download by raw url: gitd -u "https://github.com/twfb/git-directory-download"
  2. download by raw url: gitd -r -u "https://github.com/twfb/git-directory-download"
  3. dowmload by parsing: gitd -p -u "https://github.com/twfb/git-directory-download"
  4. download by raw url with proxy: gitd -r -u "https://github.com/twfb/git-directory-download" --proxy "socks5://127.0.0.1:7891"
admin
  • 169
  • 1
  • 6
3

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

App: https://downdir.vercel.app/

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

R.M. Reza
  • 725
  • 1
  • 8
  • 20
2

A straightforward answer to this is to first tortoise svn from following link.

https://tortoisesvn.net/downloads.html

while installation turn on CLI option, so that it can be used from command line interface.

copy the git hub sub directory link.

Example

https://github.com/tensorflow/models/tree/master/research/deeplab

replace tree/master with trunk

https://github.com/tensorflow/models/trunk/research/deeplab

and do

svn checkout https://github.com/tensorflow/models/trunk/research/deeplab

files will be downloaded to the deeplab folder in the current directory.

Community
  • 1
  • 1
Khan
  • 1,288
  • 12
  • 11
2

To be unique, I must say you can also download Github folders without svn, git, or any api. Github supports RAW link which you can exploit to download only those files and folders which you need.

I noticed many things. Below is my research collection:

Mechanism

  • Crawl all hyperlinks <a> from webpage and get its href="value" value

  • if href value contains "/tree/master/" or "/tree/main/" then it is folder link : https://github.com/graysuit/GithubFolderDownloader /tree/main/ GithubFolderDownloader

  • else if href value contains "/blob/master/" or "/blob/main/" then it is file link : https://github.com/graysuit/GithubFolderDownloader /blob/main/ GithubFolderDownloader.sln

  • Afterwards, replace "github.com" with "raw.githubusercontent.com" and Remove "/blob/" from file : https://raw.githubusercontent.com/graysuit/GithubFolderDownloader/main/GithubFolderDownloader.sln

  • It would become RAW link. Now you can download it.

Tool

On the basis of above research, I created a minimalist tool in C# that can grab folders. graysuit/GithubFolderDownloader

Note: I am author. You can comment if any thing missing or unclear.

Gray Programmerz
  • 479
  • 1
  • 5
  • 22
1

If you need to do it programatically and you don't want to rely on SVN, you can use GitHub API to download all the contents recursively.

For inspiration, here's my ruby gist: https://gist.github.com/cvengros/b2a7e82f66519d423b6f

pcv
  • 2,121
  • 21
  • 25
1

Use this function, the first argument is the url to the folder, the second is the place the folder will be downloaded to:

function github-dir() {
    svn export "$(sed 's/tree\/master/trunk/' <<< "$1")" "$2"  
}
HappyFace
  • 3,439
  • 2
  • 24
  • 43
1

The easiest way is to use fetcher

first, install fetcher with the following command:

npm install -g github-files-fetcher

then you can download file or folder with its URL:

fetcher --url=resource_url --out=output_directory

for example:

fetcher --url="https://github.com/Gyumeijie/github-files-fetcher/blob/master/CHANGELOG.md" --out=/tmp

fatemeakbari
  • 141
  • 2
  • 6
1

You can try using the gitzip extension for chrome, its super easy to use and has helped me download many folders before too!

ssomename
  • 505
  • 6
  • 21
1

Download git repository folder to a current directory and delete git files.

#!/bin/sh    

function download_git_folder() {
  repo_url=$1
  branch=$2
  repo_subfolder_path=$3
  
  repo_folder=$(basename $repo_url)
  git init
  git remote add -f origin ${repo_url}
  git config core.sparseCheckout true
  echo "${repo_subfolder_path}" >> .git/info/sparse-checkout
  git pull origin ${branch}
  mv "${repo_subfolder_path}"/* ./

  readarray -td/ root_subfolder <<<"${repo_subfolder_path}"; declare -p root_subfolder;
  rm -rf ./.git ${root_subfolder[0]}
}

Usage

download_git_folder "git@github.com:foobar/Test.git" "master" "Test/bar" 
angordeyev
  • 511
  • 3
  • 11
1

The easiest way I found was How do I clone a subdirectory only of a Git repository?

I added -b <branch> --single-branch to the git clone command to download a specific branch. So the command I used was:

git clone --depth 1 --single-branch -b <branch> --filter=blob:none --sparse <url>
git sparse-checkout set <directory>
AndresB
  • 31
  • 5
0

If you want to use Python and SVN to download a specific GitHub directory, here is the code that you would use:

import validators
from svn.remote import RemoteClient

def download_folder(url):
    if 'tree/master' in url:
        url = url.replace('tree/master', 'trunk')

    r = RemoteClient(url)
    r.export('output')


if __name__ == '__main__':
    url = input('Enter folder URL: ')
    if not validators.url(url):
        print('Invalid url')
    else:
        download_folder(url)

You can check more details about this code and other GitHub search and download tips in this tutorial: https://python.gotrained.com/search-github-api/

GoTrained
  • 158
  • 1
  • 7
-1

This answer if for a special case when you want a certain file from a repository.

A short answer can be found here. You should change the url to this format:

https://raw.github.com/user/repository/branch/file.name

To explain it simply, enter your desired url from github. Put raw. before github in url address, and delete blob from address. For example, suppose you want to grab the csv file in this address:

https://github.com/CSSEGISandData/COVID-19/blob/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv

you should change the url to this one:

https://raw.github.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv

ETeddy
  • 115
  • 5
-7

If the directoy you want to download is a separated library, it's better to create an other git repo, and then to use the git submodule function.

Of course, you have to be the owner of the initial repo you want

Asenar
  • 6,732
  • 3
  • 36
  • 49