1

How do I obtain a single revision from a git repository without cloning the whole repository?

Note: this question has been asked 100s of times but I have yet to see an answer that solves it since they all start with "clone the whole git repository". This cannot be done in my use case.

The best I can do is:

  1. Find the depth of the commit.
  2. Clone until that depth: git clone --depth $depth $git_repo_url.
  3. Checkout and reset: git checkout $commit_hash; git reset --hard.

But this still requires cloning up to the commits depth.

Is there a way to avoid that and clone only a particular commit with depth 1?

Community
  • 1
  • 1
gnzlbg
  • 7,135
  • 5
  • 53
  • 106
  • Do you mean something like GitHub releases, i.e. download the project up to that particular commit on the master branch? – Francesco Apr 22 '16 at 15:36
  • No, with depth 1 what I mean is to download the "code" at a single particular commit. – gnzlbg Apr 22 '16 at 15:38
  • So you want to "download" only a single commit? – Francesco Apr 22 '16 at 15:39
  • Yes, I want to "download"/clone/fetch the content of a repository at a particular commit. – gnzlbg Apr 22 '16 at 15:40
  • I don't understand why you keep referring to the repository. Do you want to download the repository or a commit? or maybe just the code without the git history? – Francesco Apr 22 '16 at 15:48
  • With the "the content of the repository" I meant the content (e.g. code) without the history. – gnzlbg Apr 22 '16 at 16:11
  • Yes, it's been asked hundreds of times. Yes, all the answers tend to start with "clone the repository". The reason for that is that's the way `git` works. Continuing to ask the same question and expecting a different answer is a bit pointless. There is, however `git archive` to extract a particular revision out of an already-cloned copy of the repository... – twalberg Apr 22 '16 at 17:23
  • @twalberg The answers should then start with "No, due to the way git works that cannot be done. You could instead do x/y/z but that is not what you are asking". I find it very confusing to find the same question over and over again, and no answers that actually answer it. – gnzlbg Apr 25 '16 at 09:54

2 Answers2

2

Not with clone but with pull/fetch. See this post:

  1. How to clone git repository with specific revision/changeset?

  2. Retrieve specific commit from a remote Git repository

    git init
    git fetch --depth 1 url://to/source/repository <sha1-of-commit>
    

And set uploadpack.allowReachableSHA1InWant on the server side.

Community
  • 1
  • 1
gdlmx
  • 6,479
  • 1
  • 21
  • 39
  • The first answer there doesn't work in git > 1.4: `git fetch origin ` doesn't work. – gnzlbg Apr 22 '16 at 15:44
  • 2
    After some research, it is the server side that matters: [uploadpack.allowReachableSHA1InWant](http://stackoverflow.com/questions/14872486/retrieve-specific-commit-from-a-remote-git-repository) – gdlmx Apr 22 '16 at 16:11
  • Nice, this is exactly what I wanted. It requires git > 2.5. This is the answer. Thanks. – gnzlbg Apr 25 '16 at 10:00
0

How about if you do a reflog and git cherry-pick

Stryker
  • 5,732
  • 1
  • 57
  • 70