如何制作浅层 git 子模块?
是否可以有浅层子模块?我有一个包含多个子模块的超级项目,每个子模块都有很长的历史,因此它会不必要地拖累所有这些历史。
我所找到的只是这个未答复的线程。
我应该破解 git-submodule 实施这个?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
是否可以有浅层子模块?我有一个包含多个子模块的超级项目,每个子模块都有很长的历史,因此它会不必要地拖累所有这些历史。
我所找到的只是这个未答复的线程。
我应该破解 git-submodule 实施这个?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(10)
太长了;
(但请参阅 Ciro Santilli 答案)
或者:记录子模块应该浅克隆:
这意味着下一个 git clone --recurse-submodules 将会浅克隆子模块 '
' (深度 1 ),甚至没有--shallow-submodules
。接下来是
git submodule
/git clone
在浅层克隆方面的演变,从(2013 年)Git 1.8.4 开始,并以此为基础。即将推出的 Git 1.8.4(2013 年 7 月)< /a>:
(git 2.10 Q3 2016 允许使用 git config -f .gitmodules submodule..shallow true 来记录。
请参阅此答案的末尾)
请参阅提交275cd184d52b5b81cb89e4ec33e540fb2ae61c1f:
这意味着这是有效的:
命令可以按任何顺序运行。
git submodule
命令执行实际克隆(这次使用深度 1)。并且 git config 命令使该选项对于稍后将递归克隆存储库的其他人来说是永久的。例如,假设您有存储库
https://github.com/foo/bar
并且您想要添加https://github.com/lorem/ipsum
作为子模块,位于您的存储库中的path/to/submodule
中。命令可能如下所示:以下结果也会产生相同的结果(相反的顺序):
下次有人运行
git clone --recursive [email protected]:foo/bar.git
,它将提取https://github 的整个历史记录.com/foo/bar
,但它只会按预期浅克隆子模块。和:
atwyman 添加了在评论中:
确实如此。
也就是说,直到 git 2.8(2016 年 3 月)。在 2.8 中,即使可以从远程存储库 HEAD 之一直接访问 SHA1,
子模块 update --depth
又有了一次成功的机会。请参阅 commit fb43e31(2016 年 2 月 24 日),作者:斯特凡·贝勒 (
stefanbeller
)。帮助者:Junio C Hamano (
gitster
)。(由 Junio C Hamano --
gitster
-- 合并于 提交 9671a76,2016 年 2 月 26 日)MVG 指出在评论中提交fb43e31(git 2.9, 2016 年 2 月)
2016 年 8 月更新(3 年后)
使用 Git 2.10(2016 年第 3 季度),您将能够执行
查看“无需额外重量的 Git 子模块< /a>”了解更多。
Git 2.13(2017 年第 2 季度)请添加 commit 8d3047c(2017 年 4 月 19 日),作者:塞巴斯蒂安·舒伯特 (
sschuberth
)。(由 Sebastian Schuberth --
sschuberth
-- 合并于 提交 8d3047c,2017 年 4 月 20 日)但是,Ciro Santilli 在评论中添加了(以及详细信息在他的回答中)
Git 2.20(2018 年第 4 季度)改进了子模块支持,当工作中缺少
.gitmodules
文件时,子模块支持已更新为从HEAD:.gitmodules
处的 blob 读取树。请参阅提交 2b1257e、提交 76e9bdc(2018 年 10 月 25 日),以及 提交 b5c259f,提交 23dd8f5,提交 b2faad4, 提交 2502ffc,提交 996df4d,提交 d1b13df,提交 45f5ef3、提交 bcbc780(2018 年 10 月 5 日) ) 作者:Antonio Ospite (
ao2
)。(由 Junio C Hamano --
gitster
-- 合并于 提交 abb4824,2018 年 11 月 13 日)注意:Git 2.24(2019 年第 4 季度)修复了浅克隆子模块时可能出现的段错误。
请参阅 提交 ddb3c85(2019 年 9 月 30 日),作者:Ali Utku Selen (
auselen
)。(由 Junio C Hamano --
gitster
-- 合并于 提交 678a9ca,2019 年 10 月 9 日)Git 2.25(2020 年第一季度),澄清了
git 子模块更新
文档。请参阅 提交 f0e58b3(2019 年 11 月 24 日),作者:菲利普·布莱恩 (
phil-blain
)。(由 Junio C Hamano --
gitster
-- 合并于 提交 ef61045,2019 年 12 月 5 日)警告:在 Git 2.25(2020 年第 1 季度)中,“
git clone --recurse-submodules
”和备用对象存储之间的交互设计不当。请参阅 提交 4f3e57e、提交 10c64a0(2019 年 12 月 2 日),作者:Jonathan Tan (
jhowtan
)。(由 Junio C Hamano --
gitster
-- 合并于 提交 5dd1d59,2019 年 12 月 10 日)详细信息请参阅:
在 Git 2.25(2020 年第一季度)中,“git clone --recurse-submodules”和备用对象存储之间的交互设计不当。
配置子模块文档现在包括:
注意:“
git 子模块更新 --quiet< /code>
"(man) 没有将安静选项传播到底层
git fetch
(man ),已使用 Git 2.32(2021 年第 2 季度)进行更正。请参阅 提交 62af4bd(2021 年 4 月 30 日),作者:尼古拉斯·克拉克 (
nwc10
)。(由 Junio C Hamano --
gitster
-- 合并于 提交 74339f8,2021 年 5 月 11 日)TLDR;
(But see caveat with Ciro Santilli answer)
Or: record that a submodule should be shallow cloned:
Which means the next
git clone --recurse-submodules
will shallow clone the submodule '<name>
' (depth 1), even without the--shallow-submodules
.What follows is the evolution of
git submodule
/git clone
when it comes to shallow clones, starting (in 2013) with Git 1.8.4, and going from there.New in the upcoming Git 1.8.4 (July 2013):
(And git 2.10 Q3 2016 allows to record that with
git config -f .gitmodules submodule.<name>.shallow true
.See the end of this answer)
See commit 275cd184d52b5b81cb89e4ec33e540fb2ae61c1f:
That means this works:
The commands can be ran in any order. The
git submodule
command perform the actual clone (using depth 1 this time). And thegit config
commands make the option permanent for other people who will clone the repo recursively later.As an example, suppose you have the repo
https://github.com/foo/bar
and you want to addhttps://github.com/lorem/ipsum
as a submodule, in your repo atpath/to/submodule
. The commands may look like like the following:The following results in the same thing too (opposite order):
The next time someone runs
git clone --recursive [email protected]:foo/bar.git
, it will pull in the whole history ofhttps://github.com/foo/bar
, but it will only shallow-clone the submodule as expected.With:
atwyman adds in the comments:
That is true.
That is, until git 2.8 (March 2016). With 2.8, the
submodule update --depth
has one more chance to succeed, even if the SHA1 is directly reachable from one of the remote repo HEADs.See commit fb43e31 (24 Feb 2016) by Stefan Beller (
stefanbeller
).Helped-by: Junio C Hamano (
gitster
).(Merged by Junio C Hamano --
gitster
-- in commit 9671a76, 26 Feb 2016)MVG points out in the comments to commit fb43e31 (git 2.9, Feb 2016)
Update August 2016 (3 years later)
With Git 2.10 (Q3 2016), you will be able to do
See "Git submodule without extra weight" for more.
Git 2.13 (Q2 2017) do add in commit 8d3047c (19 Apr 2017) by Sebastian Schuberth (
sschuberth
).(Merged by Sebastian Schuberth --
sschuberth
-- in commit 8d3047c, 20 Apr 2017)However, Ciro Santilli adds in the comments (and details in his answer)
Git 2.20 (Q4 2018) improves on the submodule support, which has been updated to read from the blob at
HEAD:.gitmodules
when the.gitmodules
file is missing from the working tree.See commit 2b1257e, commit 76e9bdc (25 Oct 2018), and commit b5c259f, commit 23dd8f5, commit b2faad4, commit 2502ffc, commit 996df4d, commit d1b13df, commit 45f5ef3, commit bcbc780 (05 Oct 2018) by Antonio Ospite (
ao2
).(Merged by Junio C Hamano --
gitster
-- in commit abb4824, 13 Nov 2018)Note: Git 2.24 (Q4 2019) fixes a possible segfault when cloning a submodule shallow.
See commit ddb3c85 (30 Sep 2019) by Ali Utku Selen (
auselen
).(Merged by Junio C Hamano --
gitster
-- in commit 678a9ca, 09 Oct 2019)Git 2.25 (Q1 2020), clarifies the
git submodule update
documentation.See commit f0e58b3 (24 Nov 2019) by Philippe Blain (
phil-blain
).(Merged by Junio C Hamano --
gitster
-- in commit ef61045, 05 Dec 2019)Warning: With Git 2.25 (Q1 2020), the interaction between "
git clone --recurse-submodules
" and alternate object store was ill-designed.See commit 4f3e57e, commit 10c64a0 (02 Dec 2019) by Jonathan Tan (
jhowtan
).(Merged by Junio C Hamano --
gitster
-- in commit 5dd1d59, 10 Dec 2019)That is detailed in:
With Git 2.25 (Q1 2020), The interaction between "git clone --recurse-submodules" and alternate object store was ill-designed.
The config submodule documentation now includes:
Note: "
git submodule update --quiet
"(man) did not propagate the quiet option down to underlyinggit fetch
(man), which has been corrected with Git 2.32 (Q2 2021).See commit 62af4bd (30 Apr 2021) by Nicholas Clark (
nwc10
).(Merged by Junio C Hamano --
gitster
-- in commit 74339f8, 11 May 2021)Git 2.9.0 支持浅层子模块直接克隆,所以现在你可以调用:
Git 2.9.0 support submodules shallow clone directly, so now you can just call:
按照 Ryan 的回答,我能够想出这个简单的脚本,它迭代所有子模块和浅层克隆它们:
Following Ryan's answer I was able to come up with this simple script which iterates through all submodules and shallow clones them:
自 Git 2.14.1 起的错误/意外/烦人行为摘要
.gitmodules 中的
shallow = true
仅影响git clone --recurse-submodules
如果远程子模块的HEAD
指向所需的提交,即使目标提交由分支指向,并且即使您放置.gitmodules 上的branch = mybranch
也是如此。本地测试脚本。 GitHub 2017-11 上的行为相同,其中
HEAD
由默认分支存储库设置控制:git clone --recurse-submodules --shallow-submodules
如果出现以下情况则失败提交既未被分支引用,也未被带有消息的标记引用:错误:服务器不允许请求未通告的对象
。本地测试脚本。 GitHub 上的相同行为:
我还在邮件列表上询问:https://marc.info /?l=git&m=151863590026582&w=2 回复是:
<块引用>
理论上这应该很容易。 :)
不幸的是,实际上并没有那么多。这是因为克隆只会获得
分支(通常是 master)的最新提示。克隆中没有机制
指定所需的确切 sha1。
有线协议支持询问精确的 sha1,因此应该涵盖这一点。
(警告:它仅在服务器运营商启用时才有效
uploadpack.allowReachableSHA1InWant github上没有AFAICT)
git-fetch 允许获取任意 sha1,因此作为解决方法,您可以运行 fetch
在递归克隆之后使用“git submodule update”,因为这将使用
在初始克隆之后获取。
TODO 测试:
allowReachableSHA1InWant
。Summary of buggy / unexpected / annoying behaviour as of Git 2.14.1
shallow = true
in.gitmodules
only affectsgit clone --recurse-submodules
if theHEAD
of the remote submodule points to the required commit, even if the target commit is pointed to by a branch, and even if you putbranch = mybranch
on the.gitmodules
as well.Local test script. Same behaviour on GitHub 2017-11, where
HEAD
is controlled by the default branch repo setting:git clone --recurse-submodules --shallow-submodules
fails if the commit is neither referenced by a branch or tag with a message:error: Server does not allow request for unadvertised object
.Local test script. Same behaviour on GitHub:
I also asked on the mailing list: https://marc.info/?l=git&m=151863590026582&w=2 and the reply was:
TODO test:
allowReachableSHA1InWant
.通读 git-submodule “source”,看起来 git submodule add 可以处理已经存在存储库的子模块。在这种情况下......
您需要确保所需的提交位于子模块存储库中,因此请确保设置适当的 --深度。
编辑:您也许能够摆脱多个手动子模块克隆,然后进行一次更新:
Reading through the git-submodule "source", it looks like
git submodule add
can handle submodules that already have their repositories present. In that case...You'll want to make sure the required commit is in the submodule repo, so make sure you set an appropriate --depth.
Edit: You may be able to get away with multiple manual submodule clones followed by a single update:
您的子模块的规范位置是否远程?如果是这样,您可以克隆一次吗?换句话说,您是否想要浅层克隆只是因为您正在遭受频繁的子模块(重新)克隆的带宽浪费?
如果您想要浅克隆来节省本地磁盘空间,那么 Ryan Graham 的答案似乎是一个不错的选择。手动克隆存储库,使其变浅。如果您认为它有用,请调整
git submodule
以支持它。发送电子邮件至列表询问相关信息(实施建议、建议界面等)。在我看来,那里的人们非常支持那些真诚地希望以建设性方式增强 Git 的潜在贡献者。如果您可以对每个子模块进行一次完整克隆(加上稍后的获取以使它们保持最新),您可以尝试使用
git submodule update
的--reference
选项> (在 Git 1.6.4 及更高版本中)引用本地对象存储(例如,制作规范子模块存储库的--mirror
克隆,然后使用--reference
在您的子模块中指向这些本地克隆)。在使用--reference
之前,请务必阅读有关git clone --reference
/git clone --shared
的内容。引用镜像唯一可能出现的问题是它们是否最终获取非快进更新(尽管您可以启用引用日志并扩展其过期窗口以帮助保留任何可能导致问题的废弃提交)。只要如果您采用类似的方法,并且有可能在工作树中进行本地子模块提交,那么创建一个自动化系统来确保签出子模块引用的关键对象不会是一个好主意。在镜像存储库中悬空(如果找到的话,将它们复制到需要它们的存储库)。
而且,就像 git clone 手册页所说的那样,如果您不理解这些含义,请不要使用
--reference
。或者,您可以使用本地镜像作为子模块的源,将镜像克隆与 git clone 的默认硬链接功能结合使用,而不是使用
--reference
。在新的超级项目克隆中,执行 git submodule init ,编辑 .git/config 中的子模块 URL 以指向本地镜像,然后执行 git submodule update 。您需要重新克隆任何现有的签出子模块才能获取硬链接。您只需下载一次到镜像中,然后从本地提取到您签出的子模块中,即可节省带宽。硬链接将节省磁盘空间(尽管提取往往会累积并在签出子模块的对象存储的多个实例之间重复;您可以定期从镜像中重新克隆签出子模块,以重新获得由硬链接)。Are the canonical locations for your submodules remote? If so, are you OK with cloning them once? In other words, do you want the shallow clones just because you are suffering the wasted bandwidth of frequent submodule (re)clones?
If you want shallow clones to save local diskspace, then Ryan Graham's answer seems like a good way to go. Manually clone the repositories so that they are shallow. If you think it would be useful, adapt
git submodule
to support it. Send an email to the list asking about it (advice for implementing it, suggestions on the interface, etc.). In my opinion, the folks there are quite supportive of potential contributors that earnestly want to enhance Git in constructive ways.If you are OK with doing one full clone of each submodule (plus later fetches to keep them up to date), you might try using the
--reference
option ofgit submodule update
(it is in Git 1.6.4 and later) to refer to local object stores (e.g. make--mirror
clones of the canonical submodule repositories, then use--reference
in your submodules to point to these local clones). Just be sure to read aboutgit clone --reference
/git clone --shared
before using--reference
. The only likely problem with referencing mirrors would be if they ever end up fetching non-fast-forward updates (though you could enable reflogs and expand their expiration windows to help retain any abandoned commits that might cause a problem). You should not have any problems as long asIf you go with something like this and there is any chance that you might carry local submodule commits in your working trees, it would probably be a good idea to create an automated system that makes sure critical objects referenced by the checked-out submodules are not left dangling in the mirror repositories (and if any are found, copies them to the repositories that need them).
And, like the
git clone
manpage says, do not use--reference
if you do not understand these implications.Alternatively, instead of
--reference
, you could use the mirror clones in combination with the default hardlinking functionality ofgit clone
by using local mirrors as the source for your submodules. In new super-project clones, dogit submodule init
, edit the submodule URLs in.git/config
to point to the local mirrors, then dogit submodule update
. You would need to reclone any existing checked-out submodules to get the hardlinks. You would save bandwidth by only downloading once into the mirrors, then fetching locally from those into your checked-out submodules. The hard linking would save disk space (although fetches would tend to accumulate and be duplicated across multiple instances of the checked-out submodules' object stores; you could periodically reclone the checked-out submodules from the mirrors to regain the disk space saving provided by hardlinking).参考如何克隆具有特定修订/变更集的git存储库?< /a>
我编写了一个简单的脚本,当您的子模块引用远离主模块时,该脚本没有问题。
此语句将获取子模块的引用版本。
它速度很快,但您无法在子模块上提交编辑(您必须在 https://stackoverflow.com/a/ 之前获取 unshallow 子模块) 17937889/3156509)
完整:
Reference to How to clone git repository with specific revision/changeset?
I have written a simple script which has no problem when your submodule reference is away from the master
This statement will fetch the referenced version of submodule.
It is fast but you cannot commit your edit on the submodule (you have to fetch unshallow it before https://stackoverflow.com/a/17937889/3156509)
in full:
我创建了一个稍微不同的版本,当它没有在前沿运行时,并非所有项目都这样做。标准子模块添加不起作用,上面的脚本也不起作用。因此,我为标签引用添加了一个哈希查找,如果没有,它将回退到完整克隆。
I created a slightly different version, for when it's not running at the bleeding edge, which not all projects do. The standard submodule additions did't work nor did the script above. So I added a hash lookup for the tag ref, and if it doesn't have one, it falls back to full clone.
子模块的浅克隆是完美的,因为它们在特定修订/变更集上进行快照。从网站下载 zip 很容易,所以我尝试了一个脚本。
git submodule deinit --all -f 清除子模块树,使脚本可以重用。
git submodule
检索 40 个字符的 sha1,后跟与.gitmodules
中相同的路径相对应。我使用 perl 连接这些信息,并用冒号分隔,然后使用变量转换将值分成mysha
和mysub
。这些是关键键,因为我们需要下载 sha1 以及关联 .gitmodules 中
url
的路径。给定一个典型的子模块条目:
path =
上的myurl
键,然后查看 2 行以获取值。此方法可能无法始终如一地发挥作用,需要改进。 url grep 通过匹配最后一个/
以及.
之前的任何内容来去除任何剩余的.git
类型引用。mydir
是mysub
减去最后的/name
,后者是通向子模块名称的目录。接下来是一个
wget
,其格式为可下载的 zip 存档 url。这将来可能会改变。将文件解压缩到
mydir
,它是子模块路径中指定的子目录。生成的文件夹将是url
-sha1
的最后一个元素。检查子模块路径中指定的子目录是否存在并将其删除以允许重命名提取的文件夹。
mv
将包含 sha1 的提取文件夹重命名为其正确的子模块路径。删除下载的 zip 文件。
子模块 init
这更多的是一个 WIP 概念证明,而不是一个解决方案。当它工作时,结果是指定变更集的子模块的浅克隆。
如果存储库将子模块重新定位到不同的提交,请重新运行脚本进行更新。
像这样的脚本唯一有用的时候是源项目的非协作本地构建。
Shallow clone of a submodule is perfect because they snapshot at a particular revision/changeset. It's easy to download a zip from the website so I tried for a script.
git submodule deinit --all -f
clears the submodule tree which allows the script to be reusable.git submodule
retrieves the 40 char sha1 followed by a path that corresponds to the same in.gitmodules
. I use perl to concatenate this information, delimited by a colon, then employ variable transformation to separate the values intomysha
andmysub
.These are the critical keys because we need the sha1 to download and the path to correlate the
url
in .gitmodules.Given a typical submodule entry:
myurl
keys onpath =
then looks 2 lines after to get the value. This method may not work consistently and require refinement. The url grep strips any remaining.git
type references by matching to the last/
and anything up to a.
.mydir
ismysub
minus a final/name
which would by the directory leading up to the submodule name.Next is a
wget
with the format of downloadable zip archive url. This may change in future.Unzip the file to
mydir
which would be the subdirectory specified in the submodule path. The resultant folder will be the last element of theurl
-sha1
.Check to see if the subdirectory specified in the submodule path exists and remove it to allow renaming of the extracted folder.
mv
rename the extracted folder containing our sha1 to its correct submodule path.Delete downloaded zip file.
Submodule init
This is more a WIP proof of concept rather than a solution. When it works, the result is a shallow clone of a submodule at a specified changeset.
Should the repository re-home a submodule to a different commit, re-run the script to update.
The only time a script like this would be useful is for non-collaborative local building of a source project.
当我无法影响主存储库的克隆时,我需要一个浅层克隆子模块的解决方案。
基于上述一种解决方案:
I needed a solution to shallow clone submodules when I can not effect on cloning of main repo.
Based on one solution above: