有没有办法使GIT拉动自动更新子模块?

有没有办法自动有git submodule update (或最好是git submodule update --init git pull完成?

寻找一个gitconfiguration设置,或者一个git别名来帮助这个。

git config --global alias.pullall '!git pull && git submodule update --init --recursive'

如果你想将parameter passing给git pull,那就用这个代替:

 git config --global alias.pullall '!f(){ git pull "$@" && git submodule update --init --recursive; }; f' 

从Git 1.7.5开始,它应该默认自动更新子模块,就像你想要的那样。

[编辑:每个评论:新的1.7.5行为是自动获取子模块的最新提交,但 更新它们(在git submodule update意义上)。 所以这个答案中的信息是相关的背景,但不是一个完整的答案本身。 您仍然需要一个别名来在一个命令中提取和更新子模块。]

默认行为“按需”是每当您提交更新子模块提交的提交时更新子模块,并且此提交不在您的本地克隆中。
您也可以在每次抓取时更新它,或从不(我假设1.7.5之前的行为)。
改变这种行为的configuration选项是fetch.recurseSubmodules

此选项可以设置为布尔值或on-demand
将其设置为布尔variables时,会将fetchpull的行为更改为设置为true时的无条件recursion子模块,或者将其设置为false时不recursion。

当设置为on-demand (默认值)时, 当其超级项目检索提交更新子模块引用的提交时fetchpull fetch 只会recursion到已填充的子模块中

看到:

  • git config手册页(1.7.5) (或最新的git config手册页 )
  • git fetch man page(1.7.5) (或者最新的git fetch man page )

了解更多信息。

 git fetch --recurse-submodules[=yes|on-demand|no] 

我很惊讶没有人提到使用git钩子来做到这一点!

只需将名为post-checkoutpost-merge文件添加到相关存储库的.git/hooks目录中,并将以下内容放入其中:

 #!/bin/sh git submodule update --init --recursive 

既然你特别要求一个别名,假设你想要这个别名,你可以创build一个别名,将它们添加到一个仓库的.git/hooks中。

凯文·巴拉德(Kevin Ballard)提出的一个别名是一个非常好的解决scheme。 为了抛出另一个选项,你也可以使用一个简单的运行git submodule update [--init]的合并后钩子。

您可以为git命令创build一个自动处理子模块更新的别名。 将以下内容添加到您的.bashrc中

 # make git submodules usable # This overwrites the 'git' command with modifications where necessary, and # calls the original otherwise git() { if [[ $@ == clone* ]]; then gitargs=$(echo "$@" | cut -c6-) command git clone --recursive $gitargs elif [[ $@ == pull* ]]; then command git "$@" && git submodule update --init --recursive elif [[ $@ == checkout* ]]; then command git "$@" && git submodule update --init --recursive else command git "$@" fi }