如何浅深度克隆深度为1的特定提交?

时间:2015-07-07 20:43:49

标签: git shallow-clone

是否可以在存储库中浅层克隆特定提交,即深度为1?像

这样的东西
git clone http://myrepo.git 728a4d --depth 1

获取存储库状态,因为它是在使用SHA 728a4d...提交的时候?

我们的动机是避免必须克隆整个存储库,然后检查该特定提交,当我们只对该特定提交时的存储库状态感兴趣时。

4 个答案:

答案 0 :(得分:37)

从Git 2.5.0开始(需要在两者客户端和服务器端都可用),您可以在服务器端设置uploadpack.allowReachableSHA1InWant=true以启用特定SHA1的提取:< / p>

git init
git remote add origin <url>
git fetch --depth 1 origin <sha1>
git checkout FETCH_HEAD

请注意,我没有找到直接使用git clone执行此操作的语法。

答案 1 :(得分:9)

注意:我的示例无法通过提交哈希来克隆,但它将有助于克隆标记并拥有轻量级存储库。

如果您的“克隆”中只有一个提交,并且您将使用提交哈希,简短答案

我在标签上使用此命令构造(在 v2.13.2.windows.1 上测试):

git clone --depth 1 git@github.com:VENDOR/REPO.git --branch 1.23.0 --single-branch

1.23.0 - 它可以是提交哈希或分支。

完整示例:

$ git clone --depth 1 git@github.com:Seldaek/monolog.git --branch 1.23.0 --single-branch
Cloning into 'monolog'...
remote: Counting objects: 201, done.
remote: Compressing objects: 100% (188/188), done.
remote: Total 201 (delta 42), reused 32 (delta 5), pack-reused 0
Receiving objects: 100% (201/201), 190.30 KiB | 0 bytes/s, done.
Resolving deltas: 100% (42/42), done.
Note: checking out 'fd8c787753b3a2ad11bc60c063cff1358a32a3b4'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

$ cd monolog

.git目录大小(使用完整clone 267K vs 2.6M ):

$ du -h --max-depth=0 .git
267K    .git

我想表示,--branch可以带标签/分支。

https://git-scm.com/docs/git-clone#git-clone---branchltnamegt

  

--branch也可以在生成的存储库中获取标记并分离该提交的HEAD。

<强> UPD

简而言之,它可以采取“参考”。 你可以在这里阅读更多: git错误消息“服务器不允许请求未声明的对象”是什么意思?

此外,没有技巧,如:

git fetch --depth 1 origin <COMMIT_HASH>

感谢@BenjiWiebe指责我的错误。

答案 2 :(得分:4)

答案是:你不能 为什么?详细解释可以在这里找到:Why Isn't There A Git Clone Specific Commit Option?

你还能做什么?

如何将存储库克隆到特定提交? (完整克隆)

# Create empty repository to store your content
git clone <url>
git reset <sha-1> --hard

更多信息:

如何克隆单个分支?

git clone <url> --branch <branch_name> --single-branch <folder_name>

如何仅克隆来自给定分支的最新提交?

git clone <url> --depth=1 --branch <branch_name> --single-branch <folder_name>

如何浅深度克隆深度为1的特定提交?

正如@sschuberth评论的那样:--depth暗示--single-branch

而不是克隆使用fetch命令:

# fetch a commit (or branch or tag) of interest
# In this case you will have the full history of this commit
git fetch origin <sha1>

答案 3 :(得分:2)

尝试在bash中使用while

git clone --depth=1 $url
i=1; while ! git show $sha1; do git fetch --depth=$((i+=1)); done

这非常慢,因为它单独提取每个提交;你可以增加增量(分批获取提交并提高网络性能),但它仍然是一种蛮力的方法。