我有一个带有Git子模块的项目。它来自ssh://…URL,在提交a上,提交B已经被推到那个URL,我想让子模块检索提交,并更改它。

现在,我的理解是git子模块更新应该这样做,但它没有。它不做任何事情(没有输出,成功退出代码)。这里有一个例子:

$ mkdir foo
$ cd foo
$ git init .
Initialized empty Git repository in /.../foo/.git/
$ git submodule add ssh://user@host/git/mod mod
Cloning into mod...
user@host's password: hunter2
remote: Counting objects: 131, done.
remote: Compressing objects: 100% (115/115), done.
remote: Total 131 (delta 54), reused 0 (delta 0)
Receiving objects: 100% (131/131), 16.16 KiB, done.
Resolving deltas: 100% (54/54), done.
$ git commit -m "Hello world."
[master (root-commit) 565b235] Hello world.
 2 files changed, 4 insertions(+), 0 deletions(-)
 create mode 100644 .gitmodules
 create mode 160000 mod
# At this point, ssh://user@host/git/mod changes; submodule needs to change too.
$ git submodule init
Submodule 'mod' (ssh://user@host/git/mod) registered for path 'mod'
$ git submodule update
$ git submodule sync
Synchronizing submodule url for 'mod'
$ git submodule update
$ man git-submodule 
$ git submodule update --rebase
$ git submodule update
$ echo $?
0
$ git status
# On branch master
nothing to commit (working directory clean)
$ git submodule update mod
$ ...

我也尝试过git fetch mod,它似乎做了一个取回(但不可能,因为它不提示密码!),但git日志和git显示否认新提交的存在。到目前为止,我只是在rm-ing模块并重新添加它,但这在原则上是错误的,在实践中也很乏味。


当前回答

处理包含子模块的git项目最简单的方法是总是添加

--recurse-submodules 

在每个git命令的末尾 例子:

git fetch --recurse-submodules

另一个

git pull --update --recurse-submodules

等等……

其他回答

这适用于我更新到最新的提交

Git子模块更新——递归——远程——初始化

简单地获取子模块:

git submodule update --init --recursive

现在继续将它们更新到最新的主分支(例如):

git submodule foreach git pull origin master

这里有一个很棒的一行代码,可以将所有内容更新到master上的最新版本:

git submodule foreach 'git fetch origin --tags; git checkout master; git pull' && git pull && git submodule update --init --recursive

感谢Mark Jaquith

注意,虽然更新子模块提交的现代形式是:

git submodule update --recursive --remote --force

请参阅Gabriel Staples的另一种回答,不使用“合并”力。

——force选项允许发生签出,即使在包含存储库的索引中指定的提交已经与子模块中签出的提交匹配。

在这种情况下,——merge选项似乎没有必要:“记录在超项目中的提交将被合并到子模块中的当前分支中。”


旧的形式是:

git submodule foreach --quiet git pull --quiet origin

除了……第二种形式并不是真正的“安静”。

参见提交a282f5a (12 Apr 2019) by nguyThái ngeconc Duy (pclouds)。 (由Junio C Hamano—gitster—在commit f1c9f6c中合并,2019年4月25日)

子模块foreach:修复“<命令>—quiet”不被尊重

Robin reported that git submodule foreach --quiet git pull --quiet origin is not really quiet anymore. It should be quiet before fc1b924 (submodule: port submodule subcommand 'foreach' from shell to C, 2018-05-10, Git v2.19.0-rc0) because parseopt can't accidentally eat options then. "git pull" behaves as if --quiet is not given. This happens because parseopt in submodule--helper will try to parse both --quiet options as if they are foreach's options, not git-pull's. The parsed options are removed from the command line. So when we do pull later, we execute just this git pull origin When calling submodule helper, adding "--" in front of "git pull" will stop parseopt for parsing options that do not really belong to submodule--helper foreach. PARSE_OPT_KEEP_UNKNOWN is removed as a safety measure. parseopt should never see unknown options or something has gone wrong. There are also a couple usage string update while I'm looking at them. While at it, I also add "--" to other subcommands that pass "$@" to submodule--helper. "$@" in these cases are paths and less likely to be --something-like-this. But the point still stands, git-submodule has parsed and classified what are options, what are paths. submodule--helper should never consider paths passed by git-submodule to be options even if they look like one.


Git 2.23(2019年Q3)修复了另一个问题:当“——recursive”选项正在使用时,“Git submodule foreach”没有保护传递给命令的命令行选项,以便在每个子模块中正确运行。

参见莫里亚十四行诗(momoson)的commit 30db18b(2019年6月24日)。 (由Junio C Hamano—gitster—在commit 968eecb中合并,2019年7月9日)

子模块foreach:修复选项的递归

Calling: git submodule foreach --recursive <subcommand> --<option> leads to an error stating that the option --<option> is unknown to submodule--helper. That is of course only, when <option> is not a valid option for git submodule foreach. The reason for this is, that above call is internally translated into a call to submodule--helper: git submodule--helper foreach --recursive \ -- <subcommand> --<option> This call starts by executing the subcommand with its option inside the first level submodule and continues by calling the next iteration of the submodule foreach call git --super-prefix <submodulepath> submodule--helper \ foreach --recursive <subcommand> --<option> inside the first level submodule. Note that the double dash in front of the subcommand is missing. This problem starts to arise only recently, as the PARSE_OPT_KEEP_UNKNOWN flag for the argument parsing of git submodule foreach was removed in commit a282f5a. Hence, the unknown option is complained about now, as the argument parsing is not properly ended by the double dash. This commit fixes the problem by adding the double dash in front of the subcommand during the recursion.


请注意,在Git 2.29 (Q4 2020)之前,“Git子模块更新—quiet”(man)并没有压制底层的“rebase”和“pull”命令。

参见Theodore Dubois (tbodt)提交3ad0401(2020年9月30日)。 (由Junio C Hamano - gitster -在commit 300cd14中合并,2020年10月5日)

子模块更新:在merge/rebase下使用"——quiet" 署名:Theodore Dubois

Commands such as $ git pull --rebase --recurse-submodules --quiet produce non-quiet output from the merge or rebase. Pass the --quiet option down when invoking "rebase" and "merge". Also fix the parsing of git submodule update(man) -v. When e84c3cf3 ("git-submodule.sh: accept verbose flag in cmd_update to be non-quiet", 2018-08-14, Git v2.19.0-rc0 -- merge) taught "git submodule update"(man) to take "--quiet", it apparently did not know how ${GIT_QUIET:+--quiet} works, and reviewers seem to have missed that setting the variable to "0", rather than unsetting it, still results in "--quiet" being passed to underlying commands.


在Git 2.38 (Q3 2022)中,Git -submodule.sh已准备好被转换为内置,这意味着存在上述问题的子模块-helper将被淡出。

参见commit 5b893f7, commit 2eec463, commit 8f12108, commit 36d4516, commit 6e556c4, commit 0d68ee7, commit d9c7f69, commit da3aae9, commit 757d092, commit 960fad9, commit 8577525 (28 Jun 2022) by Ævar Arnfjörð Bjarmason (avar)。 参见commit b788fc6(2022年6月28日),作者Glen Choo (chooglen)。 (由Junio C Hamano—gitster—在commit 361cbe6中合并,2022年7月14日)

git-submodule.sh:使用$quiet,而不是$GIT_QUIET 署名:Ævar Arnfjörð Bjarmason

Remove the use of the "$GIT_QUIET" variable in favor of our own "$quiet", ever since b3c5f5c ("submodule: move core cmd_update() logic to C", 2022-03-15, Git v2.36.0-rc0 -- merge) we have not used the "say" function in git-sh-setup.sh, which is the only thing that's affected by using "GIT_QUIET". We still want to support --quiet for our own use though, but let's use our own variable for that. Now it's obvious that we only care about passing "--quiet" to git submodule--helper, and not to change the output of any "say" invocation.

如果你不知道主机分支,这样做:

git submodule foreach git pull origin $(git rev-parse --abbrev-ref HEAD)

它将获得主Git存储库的一个分支,然后为每个子模块提取相同的分支。