Generally speaking you can use git-submodule foreach
to do stuff iteratively to each submodule. You could run your foreach, and do something like this to compare HEAD to master
$(git rev-parse --abbrev-ref HEAD) == $(git rev-parse --abbrev-ref master)
However, you may also be interested to know that you can tell submodules not to detach their HEADs when updating by defining the update property in .gitmodules
. Find the submodule relevant submodule in .gitmodules
and add an update = rebase
or update = merge
under it. This will mean when when you a submodule update, git will go into the submodule and instead of just checking out the commit directly (which detaches HEAD), it will perform either a pull or rebase.
Documentation is a bit shotty, but take a look at submodule.$name.update option, the --merge and --rebase update flags, and the --branch flag.
https://www.kernel.org/pub/software/scm/git/docs/git-submodule.html