fetch git repo at specific commit without cloning

3.2k views Asked by At

I am trying to get a git repo at a specific commit hash without cloning! Every example wants to clone the whole repo. This makes sense but the repo I have in mind is huge and I need this to leave a small footprint as it is going with a docker image.

The commit hash is in the URL - either wget or curl could/should recursively fetch but I have a feeling github is blocking that as all I ever get is robots.txt

The repo and commit:

https://github.com/phalcon/cphalcon/tree/1d6d21c98026b5de79ba5e75a3930ce7d1ebcd2e

my best attempt error:

git fetch https://github.com/phalcon/cphalcon/ 1d6d21c98026b5de79ba5e75a3930ce7d1ebcd2e
error: Server does not allow request for unadvertised object 1d6d21c98026b5de79ba5e75a3930ce7d1ebcd2e

update

Suggestions of answers that use clone aren't answering the question. I can clone/checkout no problem. Trying to do it without having the whole repo locally

1

There are 1 answers

1
torek On

You will need a Git repository, for the reason shown in the error message:

error: Server does not allow request for unadvertised object ...

An "unadvertised object" is one that does not have a name directly attached to it. (If the object had a name, you could ask for the named thing, and the server would give you the object. This assumes that a complaint like "I don't allow blue cats here" implies "I do allow some other colors of cats", which isn't necessarily true either, but it's kind of a reasonable assumption.1) This means you must obtain the object by some indirect means, i.e., using git fetch.

The repository you need is, by definition, a clone—or at least a partial clone—of the original repository. You can use a shallow clone, which is one that is incomplete. Precisely what depth is necessary is something you cannot compute in advance: someone who has a full clone could figure it out, and once you have a deep enough clone, you can find the exact number. But if your clone is too shallow, all you know is that your clone is too shallow.

See also Git fetch a specific commit by hash and git shallow clone (clone --depth) misses remote branches. Note that you may or may not want --single-branch. Clone with some depth and see if it's enough, and if not, raise the depth with git fetch --depth <bigger-number> until it is enough.


1In fact, this logic appears to be the true origin for the phrase "the exception that proves the rule": it comes from the Latin phrase Exceptio probat regulam in casibus non exceptis. See https://pocketbookuk.com/tag/cicero/ and this answer on english.stackexchange.com for more on this.