425

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?

yuit
  • 5,209
  • 4
  • 17
  • 6
  • 2
    Git 1.9/2.0 (Q1 2014) will be much more efficient with shallow cloning: http://stackoverflow.com/a/21217267/6309 and http://stackoverflow.com/a/21217326/6309 – VonC Jan 19 '14 at 13:28

2 Answers2

626

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
ma11hew28
  • 106,283
  • 107
  • 420
  • 616
Greg Hewgill
  • 828,234
  • 170
  • 1,097
  • 1,237
  • 91
    example: `git clone --depth=1 ` – iDev247 Jan 15 '13 at 23:01
  • 14
    Since [commit 82fba2b](https://github.com/git/git/commit/82fba2b9d39163a0c9b7a3a2f35964cbc039e1a) in git 1.9 these limitations no longer exist. – niutech Mar 05 '14 at 13:26
  • @charles I tested it successfully against HTTP with git 1.9.1 and onward. – bufh Sep 07 '16 at 06:30
  • 1
    What limitations? – Ignorant Sep 11 '16 at 10:38
  • 4
    @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. – odinho - Velmont Sep 14 '16 at 09:54
  • @iDev247 ok. But does that statement keep the history just-1-commit long when I `pull`, or just updates the history from the grafted point? – floatingpurr Mar 09 '17 at 13:28
  • 1
    The option [was added in `git 1.8.4`](https://github.com/git/git/commit/275cd184d52b5b81cb89e4ec33e540fb2ae61c1f). – ivan_pozdeev Dec 24 '17 at 15:35
  • You can combine this with `--branch ` to get a tagged commit. – z0r Nov 28 '19 at 23:23
84

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.

Jakub Narębski
  • 268,805
  • 58
  • 209
  • 228
  • 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. – VasiliNovikov Sep 19 '17 at 22:21