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

Chi*_*hin 55 git shallow-clone

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

git clone http://myrepo.git 728a4d --depth 1
Run Code Online (Sandbox Code Playgroud)

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

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

ssc*_*rth 49

从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
Run Code Online (Sandbox Code Playgroud)

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

  • 正确,GitHub只启用了`allowTipSHA1InWant`,但既没有'allowReachableSHA1InWant`也没有'allowAnySHA1InWant`. (7认同)
  • 现在这可以与 GitHub 配合使用。 (7认同)
  • 注意,您必须提供*整个* sha1 提交,而不是缩写! (6认同)
  • 它至少不适用于 github。我无法获取 https://github.com/Seldaek/monolog/commit/d6be96bc3c570597c67b5a83ca7968de392a75ac。`git fetch --depth 1 origin d6be96bc...` 显示 `fatal: 找不到远程引用 d6be96bc...` (4认同)
  • 尝试了一下,我发现它只适用于非常现代的 git 客户端版本。即使 git 2.17 也不适合我。我在 2.24 重建了 git 并且成功了。 (2认同)
  • 在 https://github.com/dotnet/aspnetcore/ @ 62c098bc170f50feca15916e81cb7f321ffc52ff 上为我使用 2.27.0.windows.1 (2认同)

Kir*_*rby 15

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

如果你的"clone"中只有一个提交,并且你将使用commit hash,那么简短的回答是 NO.

我使用这个命令构造(在v2.13.2.windows.1测试)来标记:

git clone --depth 1 git@github.com:VENDOR/REPO.git --branch 1.23.0 --single-branch
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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

$ du -h --max-depth=0 .git
267K    .git
Run Code Online (Sandbox Code Playgroud)

我想表示,--branch可以采取标签/分支.

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

--branch 也可以在结果存储库中的提交处获取标记并分离HEAD.

UPD

简而言之,它可以采取"参考".您可以在这里阅读更多内容:git错误消息"服务器不允许请求未发布的对象"是什么意思?

此外,没有像这样的技巧:

git fetch --depth 1 origin <COMMIT_HASH>
Run Code Online (Sandbox Code Playgroud)

谢谢@BenjiWiebe指责我的错误.

  • 一百万赞成票。太多人声称这是不可能的。 (3认同)
  • `--branch`不支持提交,仅支持分支名称...根本解决不了。事实并非如此。 (2认同)
  • Kirby,我现在正在尝试这样做,但正如 @abourget 所说,它对于“提交”“不起作用”。也许它适用于标签和分支,但我试图用提交哈希来做到这一点,但这不起作用。 (2认同)

Cor*_*rin 7

尝试while在 bash 中使用:

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

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


Cod*_*ard 5

直接的答案是:你不能。
为什么?详细解释可以在这里找到:为什么没有 Git 克隆特定提交选项?

你还能做什么?

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

# Create empty repository to store your content
git clone <url>
git reset <sha-1> --hard
Run Code Online (Sandbox Code Playgroud)

更多信息:

如何克隆单个分支?

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>
Run Code Online (Sandbox Code Playgroud)

  • 这并没有回答这个问题。提问者想知道如何检出单个提交,即使没有 ref 直接指向它。 (5认同)