有没有办法让 git pull 自动更新子模块?
自动让 git submodule update
(或者最好是在 git pull
完成时调用 git submodule update --init
?
有没有办法 git 配置设置,或 git 别名来帮助解决这个问题。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
从 Git 2.14 开始,您可以使用 git pull --recurse-submodules (并将其别名为您喜欢的任何名称)。
从 Git 2.15 开始,您可以设置
submodule.recurse
设置为 true 以启用所需的行为。您可以通过运行以下命令在全局范围内执行此操作:
As of Git 2.14, you can use
git pull --recurse-submodules
(and alias it to whatever you like).As of Git 2.15, you could set
submodule.recurse
to true to enable the desired behaviour.You can do this globally by running:
git config --global alias.pullall '!git pull && git submodule update --init --recursive'
如果您希望将参数传递给 git pull,请改用:
git config --global alias.pullall '!git pull && git submodule update --init --recursive'
If you want arguments to be passed to git pull, then use this instead:
从 Git 1.7.5 开始,它应该像您希望的那样默认自动更新子模块。
[编辑:根据评论:新的 1.7.5 行为是自动获取子模块的最新提交,但不更新它们(在
git 子模块更新
意义上)。因此,该答案中的信息与背景相关,但其本身并不是完整的答案。您仍然需要一个别名来在一个命令中拉取和更新子模块。]默认行为“按需”是每当您获取更新子模块提交的提交时更新子模块,并且此提交尚未位于您的本地克隆。
您还可以在每次获取时更新它或从不更新它(我假设是 1.7.5 之前的行为)。
更改此行为的配置选项是
fetch.recurseSubmodules
。请参阅:
git 配置 手册页 (1.7.5)
(或最新的
git config 手册页
)
git fetch
手册页 (1.7.5) (或最新的 git fetch 手册页)了解更多信息。
Starting with Git 1.7.5 it should update submodules automatically by default like you want it to.
[EDIT: per comments: the new 1.7.5 behaviour is to automatically fetch the latest commits for submodules, but not to update them (in the
git submodule update
sense). So the information in this answer is relevant as background, but is not a complete answer by itself. You still need an alias to pull and update submodules in one command.]The default behavior, "on-demand", is to update submodules whenever you fetch a commit that updates the submodule commit, and this commit isn't already located in your local clone.
You can also have it updated on every fetch or never (pre-1.7.5 behavior I assume).
The config option to change this behavior is
fetch.recurseSubmodules
.See:
git config
man page (1.7.5) (or latestgit config
man page)git fetch
man page (1.7.5) (or latest git fetch man page)for more information.
我很惊讶没有人提到使用 git hooks 来做到这一点!
只需将名为
post-checkout
和post-merge
的文件添加到相关存储库的.git/hooks
目录中,并将以下内容放入每个文件中:其中:由于您特别要求提供别名,假设您希望许多存储库都拥有此别名,那么您可以创建一个别名,将这些别名添加到存储库的
.git/hooks
中。I'm surprised nobody mentioned using git hooks to do this!
Just add files named
post-checkout
andpost-merge
to your.git/hooks
directory of the relevant repositories, and put the following into each of them:Since you specfically asked for an alias, assuming you want to have this for many repositories, you can create an alias which adds these to a repository's
.git/hooks
for you.正如其他人提到的,您可以轻松地设置它:
但是,如果您像我一样并且有更复杂的
.gitconfig
设置(我的主~/.gitconfig
文件使用include
加载其他.gitconfig
文件),并且你永远记不住如何在命令行git
配置格式和.gitconfig
格式,以下是将其添加到任何.gitconfig
文件中的方法:As others have mentioned, you can easily set this with:
However, if you're like me and have a more complex
.gitconfig
setup (my main~/.gitconfig
file usesinclude
to load in other.gitconfig
files), and you can never remember how to convert between the command-linegit
config format and the.gitconfig
format, here's how to add it to any of your.gitconfig
files:正如凯文·巴拉德(Kevin Ballard)建议的那样,别名是一个完美的解决方案。只是为了抛出另一个选项,您还可以使用合并后挂钩,它只运行 git submodule update [--init] 。
An alias, as suggested by Kevin Ballard, is a perfectly good solution. Just to toss another option out there, you could also use a post-merge hook which simply runs
git submodule update [--init]
.您可以为自动处理子模块更新的 git 命令创建别名。将以下内容添加到您的 .bashrc
You can create an alias for the git command that automatically handles submodule updating. Add the following to your .bashrc
事实上,你不必这样做。
Git 2.34(2021 年第 4 季度)之前,“
git clone --recurse-submodules
"(man),所有子模块都会被克隆,但默认情况下它们不会被其他命令递归到。使用 Git 2.34 和
submodule.stickyRecursiveClone
配置集,在使用“clone”创建的存储库中,submodule.recurse
配置设置为true
--recurse-submodules
”选项。请参阅 提交 48072e3(2021 年 8 月 14 日),作者:Mahi Kolla (
24mahik
)。(由 Junio C Hamano --
gitster
-- 合并于 提交 6d09fc5,2021 年 9 月 10 日)警告:使用 Git 2.37(2022 年第 3 季度):
“
git pull
"(man)< /sup> 没有--recurse-submodules=
导致submodule.recurse
错误地优先于fetch.recurseSubmodules
,这导致了已使用 Git 2.37(2022 年第 3 季度)进行更正。请参阅 提交 5819417(2022 年 5 月 10 日),作者:Glen Choo (
chooglen
)。(由 Junio C Hamano --
gitster
-- 合并于 提交 ed54e1b,2022 年 5 月 20 日)Actually, you won't have to do that.
Before Git 2.34 (Q4 2021), after "
git clone --recurse-submodules
"(man), all submodules are cloned but they are not by default recursed into by other commands.With Git 2.34, and
submodule.stickyRecursiveClone
configuration set,submodule.recurse
configuration is set totrue
in a repository created by "clone" with "--recurse-submodules
" option.See commit 48072e3 (14 Aug 2021) by Mahi Kolla (
24mahik
).(Merged by Junio C Hamano --
gitster
-- in commit 6d09fc5, 10 Sep 2021)Warning: use Git 2.37 (Q3 2022):
"
git pull
"(man) without--recurse-submodules=<arg>
madesubmodule.recurse
take precedence overfetch.recurseSubmodules
by mistake, which has been corrected with Git 2.37 (Q3 2022).See commit 5819417 (10 May 2022) by Glen Choo (
chooglen
).(Merged by Junio C Hamano --
gitster
-- in commit ed54e1b, 20 May 2022)我能够更新子模块和嵌套子模块的唯一方法:
由于括号,我很难通过终端创建别名,所以我必须手动将其添加到 .gitconfig 中以进行全局:
有关如何运行命令的任何建议或者自动别名?
Only way how I was able to get the submodules and nested submodules to update:
I was struggling to create the alias through terminal due to the brackets so I had to manually add this to .gitconfig for global:
Any suggestions for how to run the commands or the alias automatically?