Using git to get just the latest revision

Git

Git Problem Overview


I want to track a project that uses git. I don't want to clone the full repository and the full history, I just want the latest revision, and I want to be able to update to new revisions from the remote project.

I have tried using git clone, but this creates a copy of the entire repository (huge file size), and tracking changes makes the disk space even bigger (100mb of files now takes up over 2gb).

I'm not going to be submitting patches, and I don't need the history. I just want the latest version like in subversion.

Is this possible in git?

Git Solutions


Solution 1 - Git

Use git clone with the --depth option set to 1 to create a shallow clone with a history truncated to the latest commit.

For example:

git clone --depth 1 https://github.com/user/repo.git

To also initialize and update any nested submodules, also pass --recurse-submodules and to clone them shallowly, also pass --shallow-submodules.

For example:

git clone --depth 1 --recurse-submodules --shallow-submodules https://github.com/user/repo.git

Solution 2 - Git

Alternate solution to doing shallow clone (git clone --depth=1 <URL>) would be, if remote side supports it, to use --remote option of git archive:

$ git archive --format=tar --remote=<repository URL> HEAD | tar xf -

Or, if remote repository in question is browse-able using some web interface like gitweb or GitHub, then there is a chance that it has 'snapshot' feature, and you can download latest version (without versioning information) from web interface.

Solution 3 - Git

These days, shallow clones are recommended against in most cases.

While, for the use case you mention (download the latest version and never touch it again), git clone --depth=1 works, in the more general case, it can create problems. For instance, if you want to keep your clone up to date with upstream, git fetch is much more expensive on a shallow clone.

If what you want is to download less data, partial clones are better for the general case:

git clone --filter=tree:0 <url>

This will still download the commit history, but it won't download file trees and file contents for previous commits. Fetches to upstream commits will still be cheap.

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
QuestionyuitView Question on Stackoverflow
Solution 1 - GitGreg HewgillView Answer on Stackoverflow
Solution 2 - GitJakub NarębskiView Answer on Stackoverflow
Solution 3 - GitNarrateur du chaosView Answer on Stackoverflow