应该先执行“git submodule update”还是“git submodule init”?

问题描述 投票:0回答:2

根据 此 GitHub 问题

# run git submodule update and the && makes sure init is only ran if the first worked
git submodule update && git submodule init  
# https://github.com/UCSD-PL/proverbot9001/issues/73#issue-1570685156

但是,我相信以下是正确的,或者更安全,或者标准的方式:

# - git submodule init initializes your local configuration file to track the submodules your repository uses, it just sets up the configuration so that you can use the git submodule update command to clone and update the submodules.
git submodule init
# - The --remote option tells Git to update the submodule to the commit specified in the upstream repository, rather than the commit specified in the main repository. ref: https://stackoverflow.com/questions/74988223/why-do-i-need-to-add-the-remote-to-gits-submodule-when-i-specify-the-branch?noredirect=1&lq=1
git submodule update --init --recursive --remote
# - for each submodule pull from the right branch according to .gitmodule file. ref: https://stackoverflow.com/questions/74988223/why-do-i-need-to-add-the-remote-to-gits-submodule-when-i-specify-the-branch?noredirect=1&lq=1
git submodule foreach -q --recursive 'git switch $(git config -f $toplevel/.gitmodules submodule.$name.branch || echo master || echo main )'
# - check it's in specified branch. ref: https://stackoverflow.com/questions/74998463/why-does-git-submodule-status-not-match-the-output-of-git-branch-of-my-submodule
git submodule status

我说得对吗?

交叉发布到 Reddit 的

r/git

git git-submodules
2个回答
4
投票

对于

UCSD-PL/proverbot9001
存储库,引用很多子模块(参见 its
.gitmodules
文件)的一个更简单的说明是仅使用
git clone --recurse-submodules
选项
:

这将完成所有操作:克隆父存储库,初始化所有子模块并更新它们。一气呵成。

正如“'

git submodule init
'的意义是什么?”中所解释的,当您想要所有子模块时,分开这两个步骤(初始化和更新)是有意义的(因为这会花费太多时间/资源来克隆它们),并且只想在一个子集上工作。

在这种情况下,是的,init 必须在更新之前出现。

这来自 commit be4d2c8,Git v1.5.6-rc0,2008 年 5 月

submodule update
:添加便利选项
--init

当子模块未初始化并且您不想更改子模块时 无论如何,默认为

.gitmodules
,你现在可以说

$ git submodule update --init <name>

当在未初始化的子模块上调用“

update
”而不使用
--init
时,会打印使用
--init
的提示。


1
投票

你是对的,

init
应该在
update
之前运行。

文档中所述,

init
将会

通过在 .git/config 中设置 submodule.$name.url 来初始化索引中记录的子模块(在其他地方添加并提交)。它使用 .gitmodules 中的相同设置作为模板。

换句话说,

init
设置了
update
将使用的配置。如果您没有对该配置进行任何本地修改,那么您也可以只运行
update --init
。请参阅文档

如果子模块尚未初始化,而您只想使用 .gitmodules 中存储的设置,则可以使用 --init 选项自动初始化子模块。

© www.soinside.com 2019 - 2024. All rights reserved.