git: clone specific commit id on a shallow clone
Asked Answered
I

1

6

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.)

Incorporating answered 8/9, 2017 at 13:45 Comment(3)
This falls firmly in the 'it depends' camp, do you run your own server? Or are you using a service like GitHub or BitBucket?Nidifugous
No. You need to create either a branch or a tag on that commit in the remote repository. See #26135716.Badlands
@LightBender: in my case I'm interested in local repositories and BitBucket (via http/ssh)Incorporating
N
4

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.

Nidifugous answered 9/9, 2017 at 2:25 Comment(2)
I just found https://mcmap.net/q/12956/-how-to-shallow-clone-a-specific-commit-with-depth-1 - does this approach require altering the security setting you mentioned? And what setting are you talking about exactly?Incorporating
@Incorporating That approach is the altering the setting I mentioned. :) See the git documentation for a detailed explanation.Nidifugous

© 2022 - 2024 — McMap. All rights reserved.