Git
How to Download a File from GitHub?
GitHub is a popular platform for hosting code repositories, and it’s often used by developers and teams to collaborate on projects, share code, and manage version control. Sometimes, instead of downloading an entire repository, you may need just a single file from a GitHub project.
In this post, we’ll cover several ways to download a file from GitHub, whether you’re using GitHub’s interface, third-party tools, or command-line options.
Why Download a Single File from GitHub?
Downloading a single file from GitHub can be useful if:
- You only need one specific file instead of the entire repository.
- You want to preview or test a file without downloading all project files.
- You want to quickly update or use a particular file on your own project without additional files.
Knowing how to download specific files will save you time and storage space, especially for large repositories.
1. Using GitHub’s Web Interface to Download a File
GitHub’s web interface makes it simple to download individual files without cloning the whole repository.
Step-by-Step Instructions
- Navigate to the Repository: Go to GitHub and open the repository containing the file you want to download.
- Locate the File: Browse through the repository structure to locate the file. You can use the search bar within the repository if needed.
- Open the File: Click on the file to open it. GitHub will display the file contents along with options for actions.
- Download the File:
- In the file preview, you’ll see a “Raw” button at the top right of the file view. Click Raw to open the file in its raw format.
- Once in the raw view, right-click the page and select Save as… (or similar, depending on your browser).
- Save the file to your preferred location on your computer with the desired file name.
Note: This approach works well for most text files and small files, but it may not work for large or binary files (e.g., images or compiled programs) hosted in GitHub repositories.
2. Using wget
to Download a File from GitHub (Command Line)
If you prefer command-line tools, you can use wget
to download a file from GitHub. This is especially useful for scripting and automating downloads.
Step-by-Step Instructions
- Get the Raw File URL:
- Go to the file on GitHub, click Raw, and copy the URL from your browser’s address bar. This will be the URL you’ll use with
wget
.
- Use
wget
Command:
- Open your terminal and type the following command:
wget <raw-file-url>
Replace <raw-file-url>
with the actual URL of the file you copied.
- Save the File:
- The file will download and save to the current directory. You can specify a different directory or rename the file by adding options to the
wget
command.
Tip: You may need to install
wget
if it’s not pre-installed on your system.
3. Using cURL to Download a File from GitHub (Command Line)
cURL
is another powerful command-line tool that can download files from the web, including files from GitHub.
Step-by-Step Instructions
- Copy the Raw URL:
- Like with
wget
, go to the file in GitHub, click on Raw, and copy the file’s URL.
- Run the
curl
Command:
- In your terminal, type:
curl -O <raw-file-url>
Replace <raw-file-url>
with the URL you copied.
- Save with a Custom Name (Optional):
- To save the file with a custom name, use:
curl -o custom-name <raw-file-url>
Replace custom-name
with your desired file name.
4. Using GitHub CLI to Download a File from a Repository
GitHub CLI is a tool provided by GitHub that allows you to interact with GitHub from your terminal. However, note that GitHub CLI doesn’t directly support downloading single files yet, but you can clone or download files using other methods.
Example Using GitHub CLI for Cloning
- Install GitHub CLI if it’s not already installed:
gh repo clone owner/repo-name
- Once cloned, navigate to the desired file in the repository and open or copy it locally.
While this downloads the entire repository, it provides a way to access specific files if you frequently need them in your workflow.
5. Downloading Files from GitHub Gist (Special Case)
GitHub Gist is a simplified way to share files and snippets. You can easily download single files from Gist.
Instructions
- Go to the Gist URL (e.g.,
https://gist.github.com/username/gist-id
). - Click on the file name.
- Click Raw to view the file in raw format.
- Right-click and select Save as… to download the file.
6. Using a GitHub Extension to Download Files (Browser Option)
There are browser extensions designed to add “Download” buttons to GitHub files. These can be handy if you often need to download single files.
Examples of Popular Extensions
- GitZip: A Chrome extension that lets you download files and folders from GitHub with a right-click option.
- Enhanced GitHub: Adds download links to GitHub files, making it easier to download without opening the raw view.
Important: Always ensure you’re using reputable extensions, as third-party extensions can pose security risks.
Final Thoughts
Downloading single files from GitHub can be done easily through the web interface, command-line tools, and browser extensions. Each method has its own benefits, and the best choice depends on your workflow, the type of file you’re working with, and the environment you’re in. By following these steps, you’ll be able to quickly and efficiently download specific files from GitHub, saving you time and making collaboration easier.