2

I have 6 sub modules in a git repository, says submodule1, submodule2,... submodule6. On 4 sub modules: submodule1/2/4/5 I have a branch, says featureABC, on them.

When I run the checkout command:

$ git submodule foreach git checkout featureABC

there is an errror:

error: pathspec 'featureABC' did not match any file(s) known to git
fatal: run_command returned non-zero status for submodule3

I know that submodule3/6 do not have branch featureABC and I want to ignore them when run recursive checkout. So my question is: is there any way to ignore checkout error, leave the branch of submodule3/5 as it is and continue to other submodule checkout?

QuocNg
  • 122
  • 1
  • 8

2 Answers2

2

Ignore all errors with this shell syntax:

git submodule foreach "git checkout featureABC || :"

: means "do nothing".

phd
  • 82,685
  • 13
  • 120
  • 165
  • This is exactly what I need, thank you very much! BTW, is there any way we can drop the quote? I'm trying to make an alias for this on linux shell, e.g. `alias gis='git submodule foreach git'` – QuocNg Mar 02 '22 at 03:42
  • @QuocNg "*…is there any way we can drop the quote?*" No, the entire subcommand must be passed to a shell as a whole. – phd Mar 02 '22 at 07:23
0

Is there any way we can drop the quote?
I'm trying to make an alias for this on linux shell, e.g. alias gis='git submodule foreach git'

You would need a git alias with parameters:

alias git = '!f() { git submodule foreach "git switch ${1} || :"; }'

I am using here git switch (Git 2.23, Q3 2019) instead of git checkout. git switch works only with branches, not "branches or files" like git checkout.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250