Git & GitHub Interview Question #114

How do you clone a repository that contains submodules?

Advanced GitHub Actions, Repository Internals, Submodules, LFS & Diagnostic Tools Senior Advanced

Quick Interview Answer

A repository with submodules can be cloned with --recurse-submodules so Git initializes and checks out the recorded submodule commits. If the repository was cloned normally, submodules can be initialized and updated afterward.

Detailed Explanation

A repository with submodules can be cloned with --recurse-submodules so Git initializes and checks out the recorded submodule commits. If the repository was cloned normally, submodules can be initialized and updated afterward.

The checked-out submodule commit is determined by the parent repository, so updating the submodule repository alone does not update the parent's recorded reference.

Code Example

git clone --recurse-submodules https://github.com/example/project.git

# For an existing clone:
git submodule update --init --recursive