What is a submodule?
- Git course
- Advanced
- Lesson 13/56
- 4 min
Learn to embed another repository with a submodule.
What is a submodule?
A Git submodule allows one Git repository to be used as a separate repository inside another repository. The parent repository does not directly include the submodule's entire history; instead, it stores a reference to a specific commit in the submodule. This lets you use a shared library or independent project within another project.
project/
├── app/
└── libraries/
└── shared-ui/A submodule has its own Git history and working directory. It can be updated independently of the parent repository.
Parent and nested repository relationship
A parent repository containing a submodule records which commit the submodule should use. The nested repository keeps its own files and commit history separate. When you create a new commit inside the submodule, you must also record the updated submodule reference in the parent repository.
git status
# modified: libraries/shared-ui (new commits)This structure helps you keep dependent projects on specific versions.
Practice Inspect the submodule directory
To verify that libraries/shared-ui is its own repository, enter that directory and display the Git repository root.
Starter code
# Alt modül dizinine geç
# Git depo kökünü gösterSample solution
cd libraries/shared-ui
git rev-parse --show-toplevelExpected output
Changed directory to libraries/shared-ui. Repository root displayed.
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 You want to use another Git repository inside a project while keeping its history separate. Which Git feature meets this need?
- Git reflog
- Git tag
- Git submodule Correct answer
- Git stash
Explanation: A submodule lets you use another Git repository inside the parent project with its own independent history.
2 Identify the submodule reference
Ana depo, alt modülün belirli bir ____ bilgisini saklar.
Ana depo, alt modülün belirli bir commit bilgisini saklar.
Explanation: The parent repository records which commit the submodule should use.
3 Parts of a submodule structure
| Term | Meaning |
|---|---|
Ana depo |
Stores the submodule reference |
Alt depo |
Has its own commit history |
Commit referansı |
Specifies the submodule revision |
Alt modül dizini |
Contains the nested repository files |