rogerxu / git-tips

Tips for Git
Apache License 2.0
2 stars 3 forks source link

How to download a specific commit from a Git repository #21

Open rogerxu opened 7 years ago

rogerxu commented 7 years ago

Sometimes we need to download a specific version of a project from a Git repository to do some work like build from source code. If clone the entire repository it will be very slow because we don't need the entire commit history.

GitHub provides a download as zip service for a specific version which meets our needs. But how about a Git repository accessed via SSH?

rogerxu commented 7 years ago

How do I download a specific git commit from a repository? - Stack Overflow

Do a "git export" (like "svn export")? - Stack Overflow

Excluding files from git archive exports using gitattributes

Git - git-archive Documentation

git archive [--format=<fmt>] [--list] [--prefix=<prefix>/] [<extra>]
          [-o <file> | --output=<file>] [--worktree-attributes]
          [--remote=<repo> [--exec=<git-upload-archive>]] <tree-ish>
          [<path>…​]

Example:

git archive -v --format=zip --output=archive.zip --remote=git@github.com:rogerxu/webpack-demo.git master app/images
rogerxu commented 7 years ago

GitHub

git - How to download .zip from GitHub for a particular commit sha? - Stack Overflow

Download URL

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.

git archive --remote

As far as I know Github does not allow archive --remote for HTTPS protocol. git archive works fine with GitHub, as long as you use the ssh protocol.

git archive -v --format=zip --output=archive.zip --remote=git@github.com:rogerxu/webpack-demo.git master app/images

svn export

How to download a project subdirectory from GitHub (Example)

svn export https://github.com/rogerxu/webpack-demo/trunk/app/images archive
rogerxu commented 7 years ago

Ref Mapping