如何浅克隆深度为 1 的特定提交?
How to shallow clone a specific commit with depth 1?
是否可以浅克隆存储库中的特定提交,即深度为 1?像
git clone http://myrepo.git 728a4d --depth 1
使用 SHA 获取提交时的存储库状态 728a4d...
?
当我们只对特定提交时的存储库状态感兴趣时,这样做的动机是避免必须克隆整个存储库,然后检查该特定提交。
直接的答案是:您不能直接使用 git 克隆。
为什么?可以在这里找到详细的解释: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 a commit (or branch or tag) of interest
# In this case you will have the full history of this commit
git fetch origin <sha1>
尝试在 bash 中使用 while
:
git clone --depth=1 $url
i=1; while ! git show $sha1; do git fetch --depth=$((i+=1)); done
这很慢,因为它会单独获取每个提交;您可以增加增量(以批量获取提交并提高网络性能),但这仍然是一种蛮力方法。
从 Git 2.5.0 开始(需要在 客户端和服务器端 都可用)您可以在服务器上设置 uploadpack.allowReachableSHA1InWant=true
启用特定 SHA1 的提取:
git init
git remote add origin <url>
git fetch --depth 1 origin <sha1>
git checkout FETCH_HEAD
请注意,我没有找到直接使用 git clone
执行此操作的语法。
注意:我的示例无助于通过提交哈希克隆到,但它有助于克隆标签并拥有轻量级存储库。
如果您必须在 "clone" 中只有一个提交并且您将要使用提交哈希,简短的回答是 否.
我使用这个命令构造(在 v2.13.2.windows.1 上测试)用于标签:
git clone --depth 1 git@github.com:VENDOR/REPO.git --branch 1.23.0 --single-branch
完整示例:
$ 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
目录大小(267K 对比 2.6M 使用完整 clone
):
$ du -h --max-depth=0 .git
267K .git
我想说明一下,--branch
可以带一个 tag/branch。
https://git-scm.com/docs/git-clone#git-clone---branchltnamegt
--branch
can also take tags and detaches the HEAD at that commit in the resulting repository.
UPD
简而言之,可以"refs"。
你可以在这里阅读更多:
git 错误消息“服务器不允许请求未通告对象”是什么意思?
此外,没有这样的技巧:
git fetch --depth 1 origin <COMMIT_HASH>
感谢@BenjiWiebe 指出我的错误。
是否可以浅克隆存储库中的特定提交,即深度为 1?像
git clone http://myrepo.git 728a4d --depth 1
使用 SHA 获取提交时的存储库状态 728a4d...
?
当我们只对特定提交时的存储库状态感兴趣时,这样做的动机是避免必须克隆整个存储库,然后检查该特定提交。
直接的答案是:您不能直接使用 git 克隆。
为什么?可以在这里找到详细的解释: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 a commit (or branch or tag) of interest
# In this case you will have the full history of this commit
git fetch origin <sha1>
尝试在 bash 中使用 while
:
git clone --depth=1 $url
i=1; while ! git show $sha1; do git fetch --depth=$((i+=1)); done
这很慢,因为它会单独获取每个提交;您可以增加增量(以批量获取提交并提高网络性能),但这仍然是一种蛮力方法。
从 Git 2.5.0 开始(需要在 客户端和服务器端 都可用)您可以在服务器上设置 uploadpack.allowReachableSHA1InWant=true
启用特定 SHA1 的提取:
git init
git remote add origin <url>
git fetch --depth 1 origin <sha1>
git checkout FETCH_HEAD
请注意,我没有找到直接使用 git clone
执行此操作的语法。
注意:我的示例无助于通过提交哈希克隆到,但它有助于克隆标签并拥有轻量级存储库。
如果您必须在 "clone" 中只有一个提交并且您将要使用提交哈希,简短的回答是 否.
我使用这个命令构造(在 v2.13.2.windows.1 上测试)用于标签:
git clone --depth 1 git@github.com:VENDOR/REPO.git --branch 1.23.0 --single-branch
完整示例:
$ 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
目录大小(267K 对比 2.6M 使用完整 clone
):
$ du -h --max-depth=0 .git
267K .git
我想说明一下,--branch
可以带一个 tag/branch。
https://git-scm.com/docs/git-clone#git-clone---branchltnamegt
--branch
can also take tags and detaches the HEAD at that commit in the resulting repository.
UPD
简而言之,可以"refs"。 你可以在这里阅读更多: git 错误消息“服务器不允许请求未通告对象”是什么意思?
此外,没有这样的技巧:
git fetch --depth 1 origin <COMMIT_HASH>
感谢@BenjiWiebe 指出我的错误。