522

I don't want to clone the full repository and I'm not going to be submitting patches. I do want to easily get new revisions in the future.

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).

Is this possible?

0

3 Answers 3

770

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
4
  • 6
    @Triangles: Limitations (no longer current): A shallow repository has a number of limitations (you cannot clone or fetch from it, nor push from nor into it), but is adequate if you are only interested in the recent history of a large project with a long history, and would want to send in fixes as patches. Commented Sep 14, 2016 at 9:54
  • 1
    The option was added in git 1.8.4. Commented Dec 24, 2017 at 15:35
  • You can combine this with --branch <tag> to get a tagged commit.
    – z0r
    Commented Nov 28, 2019 at 23:23
  • Note that if you do this, you'll likely not get all the branches in the repo, just main. Commented Jul 20, 2023 at 14:37
90

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.

1
  • 4
    This solution won't satisfy this requirement though: "I want to be able to update to new revisions from the remote project". Since it basically downloads the archive, it won't be able to quickly move forward a commit or two. "Shallow clone" allows that. Still, I guess this solution has its use cases, so worth mentioning. Commented Sep 19, 2017 at 22:21
32

These days, shallow clones are not recommended 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.

2
  • 10
    Good info! It's worth mentioning that the author of the linked article states that he "strongly recommends that developers do not use treeless clones for their daily work.". Instead, use blobless clones: git clone --filter=blob:none <url>
    – bernhof
    Commented Dec 14, 2021 at 19:00
  • It is also worth mentioning that filtering option may not be available on your server (as it happened in my case with Gerrit). Commented Mar 11, 2022 at 9:51

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.