157

I want to download a .zip with the source of a library hosted on github, but I don't want the master, because every time I download I could be downloading a different version.

This particular library does not have tags, so I can't use that.

So how do I download the source.zip for a specific commit sha?

2
  • 3
    This doesn't need to be tagged with php or composer-php, does it? Commented Aug 24, 2013 at 15:24
  • I assume this doesn't work for DevOps Commented Oct 11, 2022 at 9:31

4 Answers 4

294

You can put the sha that you want in the download url:

https://github.com/{username}/{projectname}/archive/{sha}.zip

As a general rule, if you have a url that works, you can replace "master" with the specific sha you want.

On unix:

wget https://github.com/{username}/{projectname}/archive/{sha}.zip

Keep in mind that if this is a private repo then wget will not work unless you pass an OAuth token as well.

Here's more info on that:

Having trouble downloading Git archive tarballs from Private Repo

Sign up to request clarification or add additional context in comments.

5 Comments

Wasn't expecting that (cool) way of doing it. Was expecting git commands ;)
Image
Check this link as well - Might be looking for this instead stackoverflow.com/a/48503019/341117
You can use the short sha too!
Even if I use wget -O to rename the archive ZIP file, when unzipped I get a big ugly SHA1 directory name. Any way to avoid this?
Note that instead of .zip you can also use a more UNIX-friendly .tar.gz which preserves executable flags, symlinks, etc.
47

Old way: When viewing the commit's code, click the button "Browse Code" on the upper right, after that click on "Download ZIP".

In 2025, this is how do it with GitHub's new interface: open the repository's page on GitHub. Click on the green button "Code", and then click "Download ZIP".

4 Comments

They have a 'Clone or Download' button now, where you can 'Download ZIP'.
"Clone or Download" is the new button that replaces the old "Clone" and "Download" buttons. The new-button doesn't do specific versions - use the way Zeki described
This worked for me in Feb 2017. It did download the repo with changes at the timestamp of the selected commit.
In 2023, this still works, but the button is now called "Code". From a branch, "Code" opens a drop-down that includes that "Download ZIP" link. For a specific commit, click on that commit, then click on "Browse Files" in the upper right corner, and then "Code" appears and provides a ZIP link pointing to the zip file for the commit.
10

This is a an old question, but wanted to mention that if you want just the commit as a patch, and not the whole repo at the time of the commit, you can use:

$ wget http://github.com/username/repo/commit/sha1.patch
#                        ^^^^^^^^ ^^^^        ^^^^
#                        change   change      change

The /commit and .patch parts being the important part.

This is particularly useful if you want to merge in a change that was reversed a while back and therefore doesn't exist in the forked repo.

Comments

6

For those who came here looking for a way to download a particular file (or directory), from a particular commit (or branch):

git_user="user-name"
git_project="project-name"
commit_or_branch="sha-id or branch name"
dir_or_file="path/to/dir-or-file"

archive_url="https://github.com/${git_user}/${git_project}/archive/${commit_or_branch}.tar.gz"

wget -O - ${archive_url} | tar xz --strip=1 "${git_project}-${commit_or_branch}/${dir_or_file}"

The advantage of downloading the archive as tar.gz, is that you can directly pipe wget into tar command, so it will extract the gz on the fly.

2 Comments

wasteful. files can be downloaded directly, subdirectories can be downloaded via github's SVN interface, see stackoverflow.com/questions/7106012/…
@MilaNautikus your link for using svn - requires SVN to be installed first, while wget is already available on most platforms. Instead of downvoting, post a useful answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.