When you clone or pull a Git repository, the entire contents of that repository are downloaded by default. Git does not allow you to download part of a repository. Using GitHub, you can download one file from a Git repository.
In this guide, we talk about how to download a single file from GitHub. We walk through two examples to help you learn how to download files from the web browser and the command line.
Download a Single File from GitHub
GitHub lets you download one file from a repository. This is a useful feature because it means you do not have to clone or retrieve an entire repository to download a particular file.
You cannot retrieve a single file using the git command line, even if your repository is hosted on GitHub. You need to use the GitHub web interface, or a direct URL to a file.
To download an individual file from a repository, first navigate to the file you want to download on the GitHub website. Then, click the “Raw” download button that appears on the top right corner of the file explorer window on your page:
In this example, we are viewing the README.md file in a repository called ck-git. When we click “Raw”, we are directed to a plain-text version of our file.
This takes us to the following URL:
https://raw.githubusercontent.com/Career-Karma-Tutorials/ck-git/master/README.md
Now that we are viewing a plain-text version of our file, we can save it like we would with any web resource. Press Ctrl-S or Cmd-S for Windows and Mac, respectively, and choose where you want to save the file that you are viewing.
Download a Single File Using a URL
We do not need the web interface to view the raw version of a file. We can manually write the URL of the file we want to retrieve.
Let’s take another look at the URL that the web interface pointed us to earlier:
https://raw.githubusercontent.com/Career-Karma-Tutorials/ck-git/master/README.md
This URL follows a standard format:
<owner-name>/<repo-name>/<branch>/<file>
We can use this format to retrieve any file from our Git archive, such as a HTML file or a markdown file. We could download a file called app.py in a folder called “main” using this URL:
https://raw.githubusercontent.com/Career-Karma-Tutorials/ck-git/master/main/app.py
This method works on both plain text and binary files. If you need to download an image, for example, you’ll be able to do so using this approach.
This approach only works for files that are public. If you want to retrieve a file from a private repository, you’ll need to download it directly from the GitHub web interface. This is because the web interface provides an access token that you need to view a private file.
Download a Single File Using Wget
We can download a single file from the command line using the wget command. This is because we can write the URL for the file we want to retrieve.
Like the last approach, you can only download a single file using wget if that file is public.
All we have to do to download a single file using wget is write a wget command:
wget -L https://raw.githubusercontent.com/Career-Karma-Tutorials/ck-git/master/main/app.py
This command retrieves the main/app.py file from our project. The -L flag instructs wget to retrieve only the file that we have specified. We could alternatively use cURL to retrieve the file we want to download.
Conclusion
You can download an individual file from a GitHub repository from the web interface, by using a URL, or from the command line.
You can only retrieve public files by URL or from the command line. This is because private files are protected by an access token that you can only retrieve by viewing a file from the web interface.
Now you have the knowledge you need to download a single file from GitHub like a pro!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.