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?

8
  • Do you mean something like GitHub releases, i.e. download the project up to that particular commit on the master branch?
    – Francesco
    Apr 22, 2016 at 15:36
  • No, with depth 1 what I mean is to download the "code" at a single particular commit.
    – gnzlbg
    Apr 22, 2016 at 15:38
  • So you want to "download" only a single commit?
    – Francesco
    Apr 22, 2016 at 15:39
  • Yes, I want to "download"/clone/fetch the content of a repository at a particular commit.
    – gnzlbg
    Apr 22, 2016 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, 2016 at 15:48

2 Answers 2

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.

3
  • The first answer there doesn't work in git > 1.4: git fetch origin <sha1> doesn't work.
    – gnzlbg
    Apr 22, 2016 at 15:44
  • 2
    After some research, it is the server side that matters: uploadpack.allowReachableSHA1InWant
    – gdlmx
    Apr 22, 2016 at 16:11
  • Nice, this is exactly what I wanted. It requires git > 2.5. This is the answer. Thanks.
    – gnzlbg
    Apr 25, 2016 at 10:00
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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