背景
某個(gè)git庫(kù)的gitmodules 文件如下:
[submodule "module1"]
path = path1
url = http://github.cn/module1.git
branch = module1
[submodule "module2"]
path = path2
url = http://github.cn/module2.git
tag = tag2
[submodule "module3"]
path = path3
url = http://github.cn/module3.git
branch = module3
需要git module 檢出的時(shí)候把module2 檢出到tag2
解決
git submodule foreach --recursive 'tag="$(git config -f $toplevel/.gitmodules submodule.$name.tag)";[[ -n $tag ]] && git reset --hard $tag || echo "this module has no tag"'
foreach [--recursive] <command>
Evaluates an arbitrary shell command in each checked out submodule.
The command has access to the variables $name, $sm_path, $displaypath, $sha1 and $toplevel:
$name is the name of the relevant submodule section in .gitmodules,
$sm_path is the path of the submodule as recorded in the immediate superproject,
$displaypath contains the relative path from the current working directory to the submodules root directory,
$sha1 is the commit as recorded in the immediate superproject,
and $toplevel is the absolute path to the top-level of the immediate superproject.
Note that to avoid conflicts with $PATH on Windows, the $path variable is now a deprecated synonym of $sm_path variable.
Any submodules defined in the superproject but not checked out are ignored by this command.
Unless given --quiet, foreach prints the name of each submodule before evaluating the command.
If --recursive is given, submodules are traversed recursively (i.e. the given shell command is evaluated in nested submodules as well).
A non-zero return from the command in any submodule causes the processing to terminate.
This can be overridden by adding || : to the end of the command.
As an example, the command below will show the path and currently checked out commit for each submodule:
git submodule foreach 'echo $path `git rev-parse HEAD`'