Submodule
- Git course
- Advanced
- Lesson 55/56
- 6 min
Add a submodule and complete the update flow.
What Is a Git Submodule?
A submodule lets you use one Git repository inside another repository as a separate repository. Instead of including the submodule's files directly in its own commit history, the main repository tracks a reference to a specific submodule commit. This is useful for including shared libraries or independently developed components.
git submodule add https://example.com/library.git libs/libraryThe submodule URL and tracked commit information are recorded in the main repository.
Practice Add a Submodule to the Repository
Add the repository at https://example.com/shared.git as a submodule in the libs/shared directory. After running the command, Git creates the submodule entry and the .gitmodules file.
Starter code
# Paylaşılan depoyu alt modül olarak ekleExpected output
Alt modül libs/shared dizinine eklendi.
Practice Initialize and Update Submodules
Assume that you have cloned the main repository on another computer. Initialize all registered submodules and update them to the commits referenced by the main repository. Include nested submodules in the operation.
Starter code
# Tüm alt modülleri başlat ve güncelleExpected output
Tüm alt modüller başlatıldı ve güncellendi.
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which command adds another Git repository to a project as a separate repository?
- git submodule remove
- git branch add
- git remote delete
- git submodule add Correct answer
Explanation: <code>git submodule add</code> adds the specified repository to the main project as a submodule.
2 Initialize and Update a Submodule
git submodule update --init --recursive____
git submodule update --init --recursiveupdate --init
Explanation: <code>git submodule update --init</code> initializes submodules that have not been initialized and checks out the commit recorded by the main repository.
3 Submodule Commands
| Term | Meaning |
|---|---|
submodule add |
Adds a new submodule |
update --init |
Initializes and updates a submodule |
--recursive |
Also processes nested submodules |
.gitmodules |
Stores submodule URLs |