tpg2114 tpg2114 - 7 days ago 5
Git Question

List submodules in a git repository

I have a git repository that has several submodules in it. How do I list the names of all the submodules after

git submodule init
has been run?

The
git submodule foreach
command could echo the names of the submodule, but that only works once they have been checked out which has not happened after the init step. There are more steps in the chain that need to happen before they can be checked out and I don't want to have to hard-wire names of submodules into the script.

So is there a git command to get the names of all currently registered but not yet checked out submodules?

Answer

You could use the same mechanism as git submodule init uses itself, namely, look at .gitmodules. This files enumerates each submodule path and the url it refers to.

For example, from root of repo, cat .gitmodules will print contents to screen (assuming you have cat).

Edit:

Because .gitmodule files have the git config format, you can use git config to parse those files:

git config --file .gitmodules --name-only --get-regexp path

Would show you all submodule entries, and with

git config --file .gitmodules --get-regexp path | awk '{ print $2 }'

you would only get the submodule path itself.