As Mark Adelsberger said, this is a pseudo-security1 feature. However, it's not actually limited to clone's --single-branch
/ --branch
options. It also applies to git fetch
.
There is a configuration knob you can set in a repository to enable things. In fact, there are two of them, both listed in the git config
documentation:
uploadpack.allowReachableSHA1InWant
Allow upload-pack
to accept a fetch request that asks for an object
that is reachable from any ref tip. However, note that calculating
object reachability is computationally expensive. Defaults to
false
. Even if this is false, a client may be able to steal objects
via the techniques described in the "SECURITY" section of the
gitnamespaces[7] man page; it’s best to keep private data in a
separate repository.
uploadpack.allowAnySHA1InWant
Allow upload-pack
to accept a fetch request that asks for any
object at all. Defaults to false
.
Since these both default tofalse
, your standard Git server won't let you do what you want. If you have access to the server itself, though, you can configure one (or both but that's redundant) to true
. Once you do that, you can:
git clone --depth 1
any branch at all
git fetch
the one commit you want, by hash ID (create a branch name if you like, or let the old FETCH_HEAD
compatibility stuff remember the hash ID)
- check out the one commit you want (now
HEAD
holds the hash ID as a detached HEAD)
and then optionally delete the one branch you cloned shallowly. (You still have a shallow repository, which you can't really deepen properly since you don't know which branch name(s) to key off of from your origin
. But no doubt the reason you're doing all this is to get just the one commit.)
(Or, see jthill's answer: git init
, etc., then fetch.)
Consider also / instead, using git archive
to make a non-Git-repo archive out of the (single) commit you want.
1Pseudo-security: because it superficially resembles security, but may not actually be secure. See Pseudo-.