How To Download Single Folder or Directory From GitHub Repository?
Last Updated : 23 Jul, 2024
GitHub is a popular platform for hosting and collaborating on code repositories. Often, you might come across a situation where you need to download just a single folder or directory from a GitHub repository rather than cloning the entire repository. This can save time and bandwidth, especially when the repository is large. This article will guide you through several methods to download a single folder or directory from a GitHub repository.
The approaches to download a single folder or directory from the GitHub repository are given below:
1. Using GitHub's Web Interface
GitHub’s web interface allows you to download individual files but not entire folders directly. However, there are ways to work around this limitation.
Steps:
- Navigate to the Folder:
- Go to the repository on GitHub.
- Navigate to the folder you want to download.
- Download Files Individually:
- Click on each file you need.
- Click the “Download” button or right-click and select “Save link as…” to download the file.
This method is feasible for a small number of files but becomes impractical for larger directories.
2. Using DownGit
DownGit is a web tool specifically designed to download a single folder or file from a GitHub repository.
Steps:
- Visit DownGit:
- Enter the URL:
- Enter the URL of the GitHub repository folder you want to download in the input box. For example:
https://github.com/octocat/Spoon-Knife/tree/master/docs
- Generate Download Link:
- Click on the “Create Download Link” button.
- After processing, a download link will be generated. Click on it to download the folder as a ZIP file.
3. Using GitHub API
For more technical users, GitHub API can be used to programmatically download a folder’s contents. This method involves using scripts to interact with the GitHub API.
Steps:
- Get the Folder Contents:
- Use the GitHub API to get the contents of the folder. Replace <user>, <repository>, and <path-to-folder> accordingly:
curl -s https://api.github.com/repos/<user>/<repository>/contents/<path-to-folder> | jq -r '.[] | select(.type=="file") | .download_url'
- Download Files:
- Download each file using wget or curl. Here’s a sample script in Bash:
mkdir -p folder
cd folder
for url in $(curl -s https://api.github.com/repos/<user>/<repository>/contents/
<path-to-folder> | jq -r '.[] | select(.type=="file") | .download_url'); do
wget $url
done
This script creates a directory named folder, navigates into it, and downloads each file from the specified folder in the repository.
4. Using Git with Sparse Checkout
Sparse checkout allows you to clone only specific parts of a repository. This method is useful for large repositories where downloading everything is not required.
Step 1: Initialize a sparse checkout
git clone --no-checkout https://github.com/OWNER/REPO.git
cd REPO
git sparse-checkout init --cone
Step 2: Set the specific folder to checkout.
git sparse-checkout set PATH/TO/FOLDER
Step 3: Checkout the folder
git checkout