有办法自动有git子模块更新(或最好是git子模块更新——init调用每当git拉?

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


当前回答

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

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

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

由于您特别要求一个别名,假设您希望为许多存储库使用这个别名,您可以创建一个别名,将这些别名添加到存储库的.git/hooks中。

其他回答

您可以为git命令创建一个别名,以自动处理子模块更新。将以下内容添加到.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
}

Kevin Ballard建议的别名是一个非常好的解决方案。只是抛出另一个选项,你也可以使用post-merge钩子,它只运行git子模块update[——init]。

Git配置——全局别名。pullall”!Git pull && Git子模块更新——init——递归

如果你想把参数传递给git pull,那么使用下面的代码:

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

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

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

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

由于您特别要求一个别名,假设您希望为许多存储库使用这个别名,您可以创建一个别名,将这些别名添加到存储库的.git/hooks中。

正如其他人提到的,你可以很容易地设置:

git config --global submodule.recurse true

然而,如果你像我一样,有一个更复杂的.gitconfig设置(我的主~/。Gitconfig文件使用include来加载其他。Gitconfig文件),你永远都不记得如何在命令行git配置格式和。Gitconfig格式之间转换,下面是如何将它添加到任何你的。Gitconfig文件中:

[submodule]
  recurse = true