我们使用git子模块来管理一些依赖于我们开发的许多其他库的大型项目。每个库都是一个单独的repo,作为子模块引入到依赖项目中。在开发过程中,我们经常想要获取每个依赖子模块的最新版本。

如何获取所有git子模块的最新更改?


当前回答

下面是从所有git存储库中提取的命令行,无论它们是否是子模块:

ROOT=$(git rev-parse --show-toplevel 2> /dev/null)
find "$ROOT" -name .git -type d -execdir git pull -v ';'

如果您在顶级git存储库中运行它,可以将“$ROOT”替换为。。

其他回答

亨里克走在正确的轨道上。git子模块foreach命令可以执行任意shell脚本。最新的两个选择可能是:

git submodule foreach git pull origin master

and:

git submodule foreach /path/to/some/cool/script.sh

这将遍历所有初始化的子模块并运行给定的命令。

git pull --recurse-submodules --jobs=10

git在1.8.5中首次学习的特性。

在修复错误之前,第一次需要运行

git子模块更新--init--递归

我编写了这个简单的shell脚本,对我来说很好。

#!/bin/bash

#git init
git init

#git clone repo including submodules
git clone --recurse-submodules https://github.com/username/project.git

#change directory -repo
cd project

#update the remote ie tag/commits
git submodule update --remote

#add commit 
git commit -a -m "commit in submodule"

#git push 
git push -u origin 

下面是从所有git存储库中提取的命令行,无论它们是否是子模块:

ROOT=$(git rev-parse --show-toplevel 2> /dev/null)
find "$ROOT" -name .git -type d -execdir git pull -v ';'

如果您在顶级git存储库中运行它,可以将“$ROOT”替换为。。

上面的答案很好,不过我们使用了git钩子来简化这一过程,但事实证明,在git2.14中,您可以将git-config子模块.recure设置为true,以便在您拉到git存储库时更新子模块。

如果所有子模块都在分支上,这会产生一个副作用,即推动所有子模块的更改,但如果您已经需要这种行为,这可以完成任务。

可通过以下方式完成:

git config submodule.recurse true