Downloading a single file or folder from a GitHub repo

GitHub itself does not support downloading a single file or folder, but there are several ways to do this. Compared to downloading a folder, downloading a file is more easier. You can download a file in a direct way. But the methods that used to download a folder apply to downloading a file as well. If this is a usual requirement, using Subversion is a recommended way.

Download a single file from a GitHub repo

Download a file with its raw URL.

Browser to the file on GitHub you are want to download, right click the raw button to get its rawURL. Use curl command line tool or something like that to download the file:

# Download a file and write it to a file
$ curl https://raw.githubusercontent.com/[username]/[repo-name]/[branch-name]/[file-path]/somefile.js > my-named-file.js

# Other usages

# Use -o to specifiy the output file name
$ curl -o my-named-file.js https://raw.githubusercontent.com/[username]/[repo-name]/[branch-name]/[file-path]/somefile.js

# Use -O to specify the file name in the end of the URL as the output file
$ curl -O https://raw.githubusercontent.com/[username]/[repo-name]/[branch-name]/[file-path]/somefile.js

# Download multiple files
$ curl –O [URL1] –O [URL2]

# Download with a proxy
$ curl -x [proxysever.test.com:3128] -O [URL]

Download a single folder from a GitHub repo

1. Use DownGit for temporary usage

If this is a temporary usage and you have not installed Subversion, DownGit is a good option to do this. This tool uses GitHub’s REST API to achieve that.

It’s super easy to use:

Go to DownloadGit, input the URL of the file or folder you want to download like https://github.com/gloomic/phaser-flappy-bird/tree/main/screenshots, click Download button.

It looks like:

DownGit

Create GitHub Resource Download Link

It also provides a option to let you to create a download link.

2. Use Subversion for usual usage

Although GitHub does not support git-archive, but it supports some Subversion features that we can use to download a file or a folder. If you have not Subversion installed, install it (For Windows, just install TortoiseSVN).

You need to change the URL to a file or folder, replace tree/[branch] with trunk:

$ svn export https://github.com/gloomic/phaser-flappy-bird/trunk/screenshots

Resources

Protecting your commit email on GitHub

If you want to protect your private email in a commit from being exposed, GitHub has provided a way to do that. GitHub supports an no-reply email address that can be specially used as your commit email address.

To use the noreply email, you need to set up both in the GitHub and in your local Git.

GitHub: Setting your commit email address provides a step-by-step instructions how to do that. It’s simple.

GitHub settings

GitHub > Settings > Emails > Check Keep my email addresses private and Block command line pushes that wxpose my email option in the bottom of the page, then it display the noreply email as below:

:ballot_box_with_check: Keep my email addresses private

We’ll remove your public profile email and use xxxxxxx+[username]@users.noreply.github.com when performing web-based Git operations (e.g. edits and merges) and sending email on your behalf. If you want command line Git operations to use your private email you must set your email in Git.

:ballot_box_with_check: Block command line pushes that expose my email

If you push commits that use a private email as your author email we will block the push and warn you about exposing your private email.

Local Git setting

Run below command to configure your commit email with the noreply email:

$ git config --global user.email xxxxxxx+[username]@users.noreply.github.com

Note:

For commits you made prior to changing your commit email address are still associated with your previous email address. That means the commit email in those commit are not protected.

Notes:

In fact, Git does not check whether the email is a valid one or not. Of course you can use a fake one, but noreply email is a better choice, it clearly indicates that.

Resources

GitHub: Setting your commit email address

Adding images to README on GitHub

If you may want to add a few images to README.md to display direct effects of a repository on GitHub, a convenient way is to reference images that are stored in the same repository. Here is a method that allows you to show an image link (although the image itself does not display directly), and this does not affect README.md display in a local repository.

Below is an example to reference screenshots/demo.png whose path is relative to the REAMDE.md file in the repository:

![Image alt text](screenshots/demo.png?raw=true)

Or with a title:

![Image alt text](screenshots/demo.png?raw=true "Image title")

Then the image shows as below in HTML source code on the repository’s page. Actually it still shows as an image link rather than directly displaying the image, but it is able to direct to a page that displays the image.

<img src="/[username]/[repo-name]/raw/master/screenshots/demo.png?raw=true" alt="GCMovie list table" style="max-width:100%;" >

The directed page URL will redirect to a link like below in the browser:

https://raw.githubusercontent.com/[usename]/[repo-name]/master/screenshots/demo.png

Using a relative link to point a file in the repository make it works both in the GitHub and in a local repository. You can put a link in the README.md to point a file:

[Contribution guidelines for this project](docs/CONTRIBUTING.md)

See Relative links and image paths in README files for more details.

Resources