Bash:Git submodule foreach?

时间:2011-12-03 01:25:15

标签: git bash foreach git-submodules

sup别名为submodule foreach 'git co master; git up' co& up分别是checkoutpull --rebase的别名。)。

如何添加条件以便子模块名称为Libraries/JSONKit时,它会检出名为experimental的分支,而不是master

3 个答案:

答案 0 :(得分:14)

将脚本passwd传递给git submodule并将其工作目录设置为给定子模块的顶部...因此您只需查看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'

答案 1 :(得分:1)

larsksanswer使用:

git submodule foreach '[ "$path" = "Libraries/JSONKit" ]

但现在应该是(2020年)

git submodule foreach '[ "$sm_path" = "Libraries/JSONKit" ]

在Git 2.26(2020年第一季度)中,大量“ git submodule foreach”已经用C语言重写,并且其文档也不断发展。

请参见commit fc1b924commit b6f7ac8(2018年5月10日)和commit f0fd0dccommit c033a2fPrathamesh Chavan (pratham-pc)(2018年5月9日)。
(由Junio C Hamano -- gitster --commit ea27893中合并,2018年6月25日)

submodule foreach:文档“ $sm_path”而不是“ $ path”

由于大写问题,使用变量'$ path'可能对用户有害,请参见64394e3ae9(“ git submodule。sh:不要在eval_gettext中使用$ path变量字符串”,2012-04-17,Git v1.7.11-rc0-merge中列出的batch #4)。
调整文档以提倡使用$sm_path,该文档包含相同的值。
我们仍然提供'path'变量,并将其记录为'sm_path'的不推荐使用的同义词

讨论过:拉姆齐·琼斯

Documentation/git-submodule#foreach现在包括:

foreach [--recursive] <command>

在每个检出的子模块中评估一个任意的shell命令。

该命令可以访问变量$name$sm_path$sha1$toplevel

  • $name.gitmodules中相关子模块部分的名称。
  • $sm_path是直接超级项目
  • 中记录的子模块的路径
  • $sha1是直接超级项目中记录的提交,并且
  • $toplevel是直接超级项目顶层的绝对路径。

请注意,为避免与Windows上的'$PATH'冲突,'$path'变量现已弃用'$sm_path'变量的同义词

答案 2 :(得分:-2)

将以下内容添加到.git/config

[alias]
    sup = "submodule foreach 'if [ $name == \"Libraries/JSONKit\" ]; then git co experimental; else git co master; fi; git up'"
相关问题