Search code examples
gitcommitgit-cloneshallow-clone

git: clone specific commit id on a shallow clone


I found a lot of questions/answers about cloning a repository and checking out a given commit ID immediately. Trivial approach:

git clone <URL> working-copy
cd working-copy; git checkout <COMMIT-ID>

With branches you can just git clone -b <BRANCH> <URL>

With branches you can also make a shallow-clone which makes cloning much faster but then you can't checkout an arbitrary ID any more.

So my question is: is there a way to make a shallow clone of a given URL/commit ID without having to create a branch on the remote?

Are there differences for different types of remote repositories? (e.g. local file system, BitBucket, GitHub, GitLab, etc.)


Solution

  • If you don't have control of the server, there won't be a way for you to do this. There is a security setting you can disable on a private server to make this work, but this is not recommended.

    As shallow clones are incomplete, a lot of features are unavailable (or at least won't work quite right) in a shallow repo. Usually this technique is used for deployment processing where the repo is very short lived.

    All that being said, in most situations where you'd want to shallow clone a single commit, a tag might be what you're looking for. git clone -b will also accept a tag, and because they are immutable, they will always resolve to the same commit.

    Most of the CI system's I've worked on building in the last couple years use branches for transient environments and tags only for permanent ones. This system has worked well for me, but is only one of several solid options.