submodule add
- Git course
- Advanced
- Lesson 14/56
- 4 min
Learn to add a submodule with git submodule add.
git submodule add
Use git submodule add to add a repository as a submodule to an existing project. Specify the nested repository URL first, followed by the target directory inside the parent repository. Git downloads the repository and creates the required submodule configuration.
git submodule add https://github.com/example/shared-ui.git libraries/shared-uiThe URL identifies the source repository, while the final argument specifies its location inside the project.
Choosing the submodule location
You can add a submodule using its default directory name or place it under a path of your choice. For example, you may want to group shared utilities under vendor/tools. Git creates the directory and places the nested repository there.
git submodule add https://github.com/example/dev-tools.git vendor/toolsInformation about the added submodule is stored in the parent repository's .gitmodules file.
Practice Add a submodule to the project
Add the repository https://github.com/example/date-utils.git as a submodule in the parent project at vendor/date-utils.
Starter code
# Alt modülü belirtilen konuma ekleExpected output
Cloning into 'vendor/date-utils'... Submodule added.
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 When adding a submodule, which information do you specify after the repository URL?
- The commit message
- The target directory path Correct answer
- The author name
- The remote branch description
Explanation: After the URL, you specify the submodule's directory path inside the parent repository.
2 Add a submodule to a specific directory
git submodule add https://github.com/example/ui-kit.git ____
git submodule add https://github.com/example/ui-kit.git packages/ui-kit
Explanation: The packages/ui-kit path after the URL specifies the submodule's location.
3 Submodule addition components
| Term | Meaning |
|---|---|
git submodule add |
Command for adding a submodule |
Depo URL'si |
Identifies the source repository |
Hedef yol |
Defines the submodule location |
.gitmodules |
Stores submodule configuration |