Explore Courses Blog Tutorials Interview Questions
0 votes
in DevOps and Agile by (29.3k points)

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?

1 Answer

0 votes
by (50.2k points)

For listing submodules, you could use the same mechanism as git submodule init, look at .gitmodules. This file enumerates each submodule path and the URL it refers to.

For example:

From the root of a repository, cat .gitmodules will print contents to the screen.

Because .gitmodule files have the Git configuration 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.

This mechanism will help you to list submodules in a git repository.

Related questions

Browse Categories