📖 Scenario: Imagine you have a main project that uses another project inside it. This other project is a separate repository. You want to keep them connected but separate, like having a toolbox inside your main toolbox.
🎯 Goal: You will learn how to add a nested repository inside a main repository using Git submodules. This helps manage projects inside projects clearly and safely.
📋 What You'll Learn
Create a main Git repository
Add a nested Git repository as a submodule
Check the submodule status
Display the list of submodules
💡 Why This Matters
🌍 Real World
Many projects depend on other projects. Using submodules helps keep these dependencies organized and separate, like having smaller toolkits inside a big toolbox.
💼 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 Git repository
Create a new directory called main_project and initialize it as a Git repository using git init.
Git
Hint
Use mkdir to make a folder, cd to enter it, and git init to start a Git repo.
2
Add a nested Git repository as a submodule
Inside main_project, add a submodule named nested_repo from the URL https://github.com/example/nested_repo.git using git submodule add.
Git
Hint
Use git submodule add <URL> <folder_name> to add the nested repo.
3
Check the submodule status
Use git submodule status inside main_project to see the current state of the submodule.
Git
Hint
Run git submodule status to see the submodule commit and path.
4
Display the list of submodules
Use git config --file .gitmodules --name-only --get-regexp path to list the paths of all submodules in main_project.
Git
Hint
This command reads the .gitmodules file to show submodule paths.
Practice
(1/5)
1. Why do Git submodules help when you want to include another repository inside your project?
easy
A. They merge the nested repository's history into the main project.
B. They keep the nested repository separate with its own history.
C. They copy all files from the nested repository permanently.
D. They delete the nested repository after adding it.
Solution
Step 1: Understand submodule purpose
Git submodules allow you to include another repository inside your project but keep it separate.
Step 2: Recognize history separation
This means the nested repo keeps its own commit history and does not merge with the main project.
Final Answer:
They keep the nested repository separate with its own history. -> Option B
Quick Check:
Submodules keep histories separate = A [OK]
Hint: Submodules keep nested repos separate, not merged [OK]
Common Mistakes:
Thinking submodules merge histories
Assuming submodules copy files permanently
Believing submodules delete nested repos
2. Which command correctly adds a Git submodule to your project?
easy
A. git submodule add https://github.com/example/repo.git
B. git add submodule https://github.com/example/repo.git
C. git clone submodule https://github.com/example/repo.git
D. git submodule clone https://github.com/example/repo.git
Solution
Step 1: Recall submodule add syntax
The correct command to add a submodule is git submodule add <repo-url>.
Step 2: Check options for correct syntax
Only git submodule add https://github.com/example/repo.git matches this syntax exactly.
Final Answer:
git submodule add https://github.com/example/repo.git -> Option A
Quick Check:
Use 'git submodule add' to add submodules = C [OK]
Hint: Add submodules with 'git submodule add' command [OK]
Common Mistakes:
Using 'git add submodule' which is invalid
Trying 'git clone submodule' which is not a command
Confusing 'git submodule clone' which doesn't exist
3. What is the output of running git submodule update --init after cloning a repository with submodules?
medium
A. It lists all submodules without downloading them.
B. It deletes all submodule directories from the project.
C. It fetches and checks out the submodule repositories at the recorded commit.
D. It merges submodule commits into the main repository.
Solution
Step 1: Understand 'git submodule update --init'
This command downloads the submodule repositories and checks out the commit recorded in the main repo.
Step 2: Compare options with command behavior
Only It fetches and checks out the submodule repositories at the recorded commit. correctly describes this behavior; others describe incorrect actions.
Final Answer:
It fetches and checks out the submodule repositories at the recorded commit. -> Option C
Quick Check:
'git submodule update --init' fetches submodules = D [OK]
Hint: Update and initialize submodules with 'git submodule update --init' [OK]
Common Mistakes:
Thinking it deletes submodules
Assuming it merges submodule commits
Believing it only lists submodules
4. You cloned a repository with submodules but forgot to run git submodule update --init. What problem will you face?
medium
A. The submodule folders will be empty or missing files.
B. The main repository will not clone properly.
C. The submodules will automatically update without any command.
D. The repository will merge submodule commits automatically.
Solution
Step 1: Understand cloning with submodules
When cloning, submodule folders exist but are empty until you initialize and update them.
Step 2: Identify the effect of missing update
Without running git submodule update --init, submodule content is not fetched, so folders are empty or incomplete.
Final Answer:
The submodule folders will be empty or missing files. -> Option A