我将sup
别名为submodule foreach 'git co master; git up'
(co
和up
分别为checkout
和pull --rebase
的别名。)。
如何添加条件,以便如果子模块名称为Libraries/JSONKit
,它签出名为experimental
的分支,而不是master
?
传递给git submodule
的脚本passwd的工作目录位于给定子模块的顶部...因此您可以简单地查看pwd
来查看您是否在特定的子模块中。但是,如果您花一些时间阅读git submodule
文档,事实证明它甚至更容易:
foreach
Evaluates an arbitrary shell command in each checked out submodule. The
command has access to the variables $name, $path, $sha1
and $toplevel: $name is the name of the relevant submodule section in
.gitmodules, $path is the name of the submodule directory
relative to the superproject, $sha1 is the commit as recorded in the superproject,
and $toplevel is the absolute path to the top-level of the superproject.
因此您可以执行以下操作:
git submodule foreach '[ "$path" = "Libraries/JSONKit" ] \
&& branch=experimental \
|| branch=master; git co $branch'
git submodule foreach '[ "$path" = "Libraries/JSONKit" ]
但现在应该是(2020年)
git submodule foreach '[ "$sm_path" = "Libraries/JSONKit" ]
[使用Git 2.26(Q1 2020),大量的[git submodule foreach
]已经用C重写,并且其文档也随之发展。
请参见git submodule foreach
之前的commit fc1b924(2018年5月10日)和commit b6f7ac8,commit f0fd0dc,commit c033a2f(2018年5月9日)。((由Prathamesh Chavan (pratham-pc
)合并于pratham-pc
,2018年6月25日)>
[Junio C Hamano --
gitster
--:文档'gitster
',而不是'$ path']由于大写问题,使用变量'$ path'可能对用户有害,请参阅commit ea27893(“
submodule foreach
.sh:请勿在submodule foreach
字符串中使用$ path变量”,2012-04- 17,Git v1.7.11-rc0-$sm_path
中列出的64394e3ae9)。调整文档以主张使用git submodule
,该文档包含相同的值。我们仍然提供'git submodule
'变量,并将其记录为'eval_gettext
'的不赞成使用的同义词。与讨论:Ramsay Jones
merge现在包括:
batch #4:
在每个检出的子模块中评估一个任意的shell命令。
该命令可以访问变量
$sm_path
,path
,sm_path
和Documentation/git-submodule#foreach
:。
- [
Documentation/git-submodule#foreach
是foreach [--recursive] <command>
中相关子模块节的名称。$name
是直接超级项目中记录的子模块的路径,$sm_path
是直接超级项目中记录的提交,而$sha1
是到直接超级项目顶层的绝对路径。注意,为避免与Windows上的'
$toplevel
'冲突,'$name
'变量现在已不再使用'.gitmodules
'变量的同义词
将以下内容添加到$sm_path
: