Search code examples
gitgit-clonegit-checkoutgit-reset

Git shallow clone specific version of remote repository


How can I clone a specific version of a certain branch of a remote repository?

I'm working with a very large git tree (Linux Kernel, to be more specific). In many cases I need to bring the kernel code to a machine where network and file system are very limited.

Usually I need to get just the code of a certain commit on a certain branch, w/o all the leading history.

So, after some digging (e.g. here, here, and in other places) I narrowed it down to this:

$ git clone --branch <remote-branch-name> --single-branch --depth 20 http://some-host:8080/some-git-repo.git
$ cd some-git-repo
$ git reset --hard <commit-ID-that-I-need>

Pros: I'm cloning the only branch that I need, and cloning a very limited amount of history.

Cons: Prior to cloning, I need to actually find out which branch has the commit ID that I need, and I also need to estimate how far is the commit from the branch tip and use this number as the "depth" parameter (I used 20 in the example).

So my question is: How can I clone a specific version of a certain branch of a remote repository directly, w/o the aforementioned cons?

I found answers saying that it can't be done, but they are rather dated. Perhaps a newer git has something that allows it?


Solution

  • This doesn't seem possible unless the server has a specifc setting in place.

    uploadpack.allowReachableSHA1InWant
    

    See "Retrieve specific commit from a remote Git repository" that I mentioned for Git 2.5 and more.

    git fetch --depth=1 ../testrepo/.git $SHA1
    git cat-file commit $SHA1