Reputation: 13
I want git to clone the specified commitId of a branch, knowing the repository address, branch, and commitId.I also want to use --branch="master" --single-branch --depth=1
to optimize the clone speed.
I googled it and found that most of the answers are to clone the full commit history for that branch (which is too large for my project) and then git checkout commitId.
I'm actually more curious as to why the git doesn't provide a parameter like this. git clone --commitId="xxxxxxxxx" --branch="master" --single-branch --depth=1
Thanks!
Upvotes: 1
Views: 75
Reputation: 488203
The -b
or --branch
option to git clone
is not necessarily a branch name. For instance:
git clone -b v2.29.2 https://github.com/git/git.git --depth 1
works fine.
Because of the above, git clone -b <hash-id>
(with or without --depth
) should also be allowed—but it isn't:
Cloning into 'git'...
warning: Could not find remote branch 898f80736c75878acc02dc55672317fcc0e0a5a6 to clone.
fatal: Remote branch 898f80736c75878acc02dc55672317fcc0e0a5a6 not found in upstream origin
There are multiple separate reasons it's not allowed, but the key one right now—which is that direct access to a commit hash ID might violate some sort of theoretical security options—is being dismantled to allow partial clones to fetch objects by ID directly. Once that's sufficiently commonly out of the way, git clone
should learn to do the above.
Until then, clone by tag (which is allowed).
Note that the:
--single-branch
flag is already implied by --depth 1
: shallow clones are by default single-branch clones. Further, when -b
specifies a tag (or, in the theoretical future, a raw commit hash), the resulting clone has no branches and is in detached HEAD state, so there is no point in specifying a branch name.
Upvotes: 3