Git

How to List Submodules in a Git Repository?

Git submodules are a reference to another Git repository at a certain snapshot in history. They are utilized when the development project becomes more complex and harder to manage. It can also be used when one project depends on the main Git repository and you want to keep modifications history separate. Moreover, these modules permit you to create many submodules in the project and list them whenever you want.

This write-up will explain different methods to list submodules in a Git repository.

How to List/Display Submodules in a Git Repository?

To list submodules in Git, multiple commands can be used, such as:

Method 1: List Submodules Using “git submodule” Command

To list all the submodules in the specified Git repository, execute the below-provided command:

git submodule

In the below output, two submodules can be seen along with their repository name and SHA hash. More specifically, the “Repo1” repository contains the “Submod” submodule and the “test_Repo” repository contains the “TestSubmod” submodule:

Method 2: List Submodules Using “git submodule–helper list” Command

Utilize the “–helper list” option with the previous command to list the submodule with mode, SHA-hash value, stage, and their path:

git submodule--helper list

In the below output:

  • First column represents the mode of submodules.
  • The SHA-hash value of submodules can be seen in the second column.
  • The 3rd column shows the stage.
  • The last column displays the submodules along their paths.

Method 3: List Submodules Using “git submodule | awk ‘{ print $2 }’” Command

To only view the names of submodules, use the “awk ‘{print $2}’” option with the same command:

git submodule | awk '{ print $2 }'

The following command can also be utilized to list only the names of the Git submodules:

git submodule --quiet foreach --recursive 'echo $name'

Another command to get only the names of submodules in the repository is given below:

grep path .gitmodules | sed 's/.*= //'

Method 4: List Submodules Using “git config –file .gitmodules –name-only –get-regexp path” Command

Run the given-provided command to show all submodules entries in the repository:

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

We have explained different ways to list submodules in a Git repository.

Conclusion

Multiple Git commands can be used to list submodules in the Git repository, such as “git submodule”, and “git submodule–helper list” commands to display detailed information about the submodule. The “git submodule | awk ‘{ print $2 }‘”, “git submodule –quiet foreach –recursive ‘echo $name’” and “grep path .gitmodules | sed ‘s/.*= //’” lists only the names of the submodules. Furthermore, to display all submodule entries, utilize the “git config –file .gitmodules –name-only –get-regexp path” command. This write-up explained different methods to list submodules in a Git repository.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.