Download a single folder or directory from a GitHub repository

Learn download a single folder or directory from a github repository with practical examples, diagrams, and best practices. Covers git, file, github development techniques with visual explanations.

Download a Single Folder or Directory from a GitHub Repository

Hero image for Download a single folder or directory from a GitHub repository

Learn various methods to selectively download a specific folder or directory from any GitHub repository without cloning the entire project.

GitHub repositories often contain many files and folders, but sometimes you only need a small subset of the project. Cloning the entire repository just to get one directory can be inefficient, especially for large projects or when bandwidth is limited. This article explores several effective methods to download a single folder or directory from a GitHub repository, ranging from simple web-based tools to command-line solutions.

Method 1: Using Online Tools (DownGit, SVN)

The simplest way to download a single folder is by using a third-party online service. These services typically generate a direct download link for the specified directory, allowing you to get a ZIP archive without any local setup. Another robust method involves leveraging Subversion (SVN) capabilities, which GitHub supports for sparse checkouts.

flowchart TD
    A[Start] --> B{Need a single folder?}
    B -->|Yes| C{Is it a public repo?}
    C -->|Yes| D[Use Online Tool (e.g., DownGit)]
    D --> E[Paste Folder URL]
    E --> F[Generate & Download ZIP]
    C -->|No| G{Have Git/SVN installed?}
    G -->|Yes| H[Use SVN Checkout]
    H --> I[Specify Repo & Folder Path]
    I --> J[Download Folder]
    G -->|No| K[Consider Manual Download or Git Clone]
    K --> L[End]
    B -->|No| M[Git Clone Entire Repo]
    M --> L

Decision flow for downloading a single GitHub folder.

Using DownGit (or similar services)

DownGit is a popular web-based tool that allows you to generate a direct download link for any public GitHub folder. You simply paste the URL of the folder, and it provides a ZIP file. While convenient, be mindful of using third-party services for sensitive repositories.

1. Navigate to the folder on GitHub

Open your web browser and go to the specific folder within the GitHub repository you wish to download. Copy the full URL from your browser's address bar.

2. Visit DownGit

Go to https://downgit.github.io/.

3. Paste the URL and download

Paste the copied GitHub folder URL into the input field on the DownGit website. Click the 'Download' button. A ZIP file containing the folder's contents will be downloaded to your computer.

Using Subversion (SVN)

GitHub repositories can be accessed via Subversion (SVN), which supports sparse checkouts. This means you can check out only specific subdirectories without downloading the entire repository. This method requires SVN to be installed on your system.

svn checkout <repository_url>/trunk/<folder_path> <local_folder_name>

Basic SVN checkout command for a single folder.

Method 2: Using Git Sparse Checkout (Advanced)

For those who prefer using Git directly and want more control, Git's sparse checkout feature allows you to clone a repository but only populate specific directories in your working tree. This is more involved but provides the most flexibility and is ideal for larger projects where you might eventually need other parts of the repository.

1. Initialize a new Git repository

Create a new empty directory and initialize a Git repository within it. This will be your working directory.

2. Configure sparse checkout

Enable sparse checkout and set the remote origin. Then, tell Git which specific folder(s) you want to include.

3. Pull the repository

Finally, pull the repository. Git will only download the contents of the specified folder(s).

Bash/Zsh

mkdir my-sparse-repo cd my-sparse-repo git init git remote add origin https://github.com/user/repo.git git config core.sparseCheckout true echo "path/to/your/folder/*" >> .git/info/sparse-checkout git pull origin master

PowerShell

New-Item -ItemType Directory -Name "my-sparse-repo" Set-Location -Path "my-sparse-repo" git init git remote add origin https://github.com/user/repo.git git config core.sparseCheckout true Add-Content -Path ".git/info/sparse-checkout" -Value "path/to/your/folder/*" git pull origin master

Method 3: Manual Download (for small folders)

For very small folders with only a few files, a manual approach might be quicker than setting up tools or commands. This involves navigating to each file and downloading it individually.

Steps for Manual Download

  1. Navigate to the folder: Browse to the desired folder on GitHub.
  2. Open each file: Click on each file within the folder.
  3. Download raw content: On the file's page, click the 'Raw' button. This will open the raw content of the file in your browser. Right-click (or use browser's save function) and select 'Save As...' to save the file to your local machine.
  4. Repeat: Do this for every file in the folder.