0
0
Gitdevops~20 mins

Adding a submodule in Git - Mini Project: Build & Apply

Choose your learning style9 modes available
Adding a Submodule in Git
📖 Scenario: You are working on a main project repository and want to include another repository as a submodule. This is like adding a small project inside your big project, so you can keep them separate but still work together.
🎯 Goal: Learn how to add a Git submodule to your main project repository and verify it is added correctly.
📋 What You'll Learn
Create a new Git repository folder called main_project
Add a submodule pointing to https://github.com/example/lib_project.git inside main_project
Verify the submodule is listed in the Git configuration
💡 Why This Matters
🌍 Real World
Many projects depend on other projects or libraries. Using Git submodules helps keep these dependencies organized and separate while working on the main project.
💼 Career
Understanding Git submodules is important for developers and DevOps engineers to manage complex projects with multiple repositories efficiently.
Progress0 / 4 steps
1
Create the main project repository
Create a new directory called main_project and initialize it as a Git repository using git init.
Git
Need a hint?

Use mkdir to create a folder, cd to enter it, and git init to start a Git repo.

2
Add the submodule to the main project
Inside the main_project directory, add a Git submodule with the URL https://github.com/example/lib_project.git and place it in a folder called lib_project using git submodule add.
Git
Need a hint?

Use git submodule add <repo-url> <folder-name> to add the submodule.

3
Initialize and update the submodule
Run git submodule init and git submodule update inside main_project to initialize and fetch the submodule content.
Git
Need a hint?

Use git submodule init to initialize and git submodule update to fetch the submodule files.

4
Verify the submodule is added
Use git config --file .gitmodules --name-only --get-regexp path inside main_project to list the submodule paths and verify lib_project is listed.
Git
Need a hint?

This command shows the submodule paths configured. You should see submodule.lib_project.path in the output.