How to download .zip from GitHub for a particular commit sha?

GitGithub

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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:

https://stackoverflow.com/questions/10046027/having-trouble-downloading-git-archive-tarballs-from-private-repo

Solution 2 - Git

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

Solution 3 - Git

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.

Solution 4 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChocoDeveloperView Question on Stackoverflow
Solution 1 - GitZekiView Answer on Stackoverflow
Solution 2 - Gituser2722591View Answer on Stackoverflow
Solution 3 - GittrojanfoeView Answer on Stackoverflow
Solution 4 - GitNoam ManosView Answer on Stackoverflow