gpt4 book ai didi

git - 如何浅克隆git中的单个分支?

转载 作者:IT王子 更新时间:2023-10-29 01:04:32 28 4
gpt4 key购买 nike

我有一个 git 存储库,我想对其进行浅拷贝,并且只提取一个分支。

This SO question说默认情况下 git clone 会 pull 所有远程分支。我只想对一个分支进行浅拷贝。

我这样做是为了部署到生产环境中。一个完整的 checkout 超过 400MB,但是一个 head 的 git archive 只有 16MB。看起来克隆的 pull 下所有分支的行为导致我的下载比需要的大得多。

最佳答案

Jakub已经提到了 shallow clone of selected branches is possible ,但做起来相当复杂。
他补充说:

Note however that because branches usually share most of their history, the gain from cloning only a subset of branches might be smaller than you think.

我要补充一点,您不应该在生产平台中安装任何 VCS 工具(您只安装/监控生产运行所必需的工具)。
所以git archive仍然是提取所需内容的最佳方式,作为存档(ziptar,一旦在生产端传输,您就可以在没有 Git 的情况下使用这种格式)


2012 年 3 月更新:

upcoming git1.7.10 (2012 年 4 月)实际上只允许克隆一个分支:

git clone --single-branch

你可以在t5500-fetch-pack.sh中看到它:

test_expect_success 'single branch clone' '
git clone --single-branch "file://$(pwd)/." singlebranch
'

该功能随后通过以下方式修复:

clone --single: 限制 fetch refspec 到 fetched branch

After running "git clone --single", the resulting repository has the usual default "+refs/heads/*:refs/remotes/origin/*" wildcard fetch refspec installed, which means that a subsequent "git fetch" will end up grabbing all the other branches.

Update the fetch refspec to cover only the singly cloned ref instead to correct this.

builtin/clone.c:正确检测从标签开始的克隆

31b808a (clone --single: limit the fetch refspec to fetched branch, 2012-09-20) tried to see if the given "branch" to follow is actually a tag at the remote repository by checking with "refs/tags/" but it incorrectly used strstr(3); it is actively wrong to treat a "branch" "refs/heads/refs/tags/foo" and use the logic for the "refs/tags/" ref hierarchy.
What the code really wanted to do is to see if it starts with "refs/tags/".


2016 年 9 月更新:git clone --single-branch --branch tag will work for chained tags在 Git 2.11+(2016 年第 4 季度)中。

关于git - 如何浅克隆git中的单个分支?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4929688/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com