A Git submodule is a way to include one Git repository inside another. It lets you keep a separate project inside your main project, like a folder that links to another repository.
Click to reveal answer
beginner
Which command adds a submodule to your Git repository?
The command git submodule add <repository-url> <path> adds a submodule at the specified path inside your project.
Click to reveal answer
intermediate
What file does Git update when you add a submodule?
Git updates the .gitmodules file. This file keeps track of the submodule's URL and path inside your project.
Click to reveal answer
intermediate
How do you initialize and fetch submodule content after cloning a repository with submodules?
Run git submodule update --init --recursive. This downloads the submodule data and sets it up inside your project.
Click to reveal answer
beginner
Why use submodules instead of copying code into your project?
Submodules keep the code separate and linked to its own repository. This makes updates easier and avoids mixing unrelated code directly into your project.
Click to reveal answer
Which command adds a submodule to your Git repository?
Agit clone --submodule <repository-url>
Bgit submodule add <repository-url> <path>
Cgit add submodule <repository-url>
Dgit init submodule <path>
✗ Incorrect
The correct command to add a submodule is git submodule add <repository-url> <path>.
What file does Git create or update when you add a submodule?
A.gitignore
B.gitconfig
CREADME.md
D.gitmodules
✗ Incorrect
Git updates the .gitmodules file to track submodule URLs and paths.
After cloning a repo with submodules, which command fetches the submodule content?
Agit fetch submodules
Bgit clone --recursive
Cgit submodule update --init --recursive
Dgit pull submodules
✗ Incorrect
Use git submodule update --init --recursive to initialize and fetch submodules.
What is a main benefit of using Git submodules?
AKeeps external projects linked but separate
BAutomatically merges all code into one repo
CDeletes old code automatically
DRuns tests on submodule code
✗ Incorrect
Submodules keep external projects linked but separate, making management easier.
Which of these is NOT true about Git submodules?
AThey copy all submodule code directly into your repo
BThey use the .gitmodules file
CThey require updating separately
DThey allow including one repo inside another
✗ Incorrect
Submodules do NOT copy code directly; they link to another repo instead.
Explain how to add a submodule to a Git repository and what files are affected.
Think about the command and the configuration file Git updates.
You got /4 concepts.
Describe the steps to initialize and update submodules after cloning a repository.
Focus on commands to get submodules ready after cloning.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of adding a submodule in a Git project?
easy
A. To merge two Git repositories into one history
B. To create a backup of the current repository
C. To delete a folder from the Git repository
D. To include another Git repository inside your project as a separate component
Solution
Step 1: Understand what a submodule is
A submodule is a way to include another Git repository inside your own project without merging histories.
Step 2: Identify the purpose of adding a submodule
It allows managing related projects separately but linked inside your main project.
Final Answer:
To include another Git repository inside your project as a separate component -> Option D
Quick Check:
Submodule = separate included repo [OK]
Hint: Submodule means adding a separate repo inside yours [OK]
Common Mistakes:
Thinking submodule merges histories
Confusing submodule with backup
Assuming submodule deletes files
2. Which of the following is the correct command to add a submodule from URL https://github.com/example/lib.git into folder lib?
easy
A. git clone submodule https://github.com/example/lib.git lib
B. git submodule add https://github.com/example/lib.git lib
C. git submodule init https://github.com/example/lib.git lib
D. git add submodule https://github.com/example/lib.git lib
Solution
Step 1: Recall the syntax for adding a submodule
The correct syntax is git submodule add <url> [path].
Step 2: Match the command with the syntax
git submodule add https://github.com/example/lib.git lib matches the syntax exactly with the URL and folder path.
Final Answer:
git submodule add https://github.com/example/lib.git lib -> Option B
Quick Check:
Use 'git submodule add URL path' [OK]
Hint: Use 'git submodule add URL path' to add submodules [OK]
Common Mistakes:
Using 'git add submodule' instead of 'git submodule add'
Confusing 'init' with 'add'
Trying to clone submodule like a normal repo
3. After running git submodule add https://github.com/example/tool.git tools, what will be the output of git status?
medium
A. Shows new submodule 'tools' added and staged for commit
B. Shows new file 'tools' as untracked
C. Shows no changes
D. Shows error: submodule path 'tools' already exists
Solution
Step 1: Understand what happens after adding a submodule
Adding a submodule creates a new entry in the .gitmodules file and stages the submodule folder.
Step 2: Check git status behavior
Git status will show the new submodule as added and staged for commit, not untracked or error.
Final Answer:
Shows new submodule 'tools' added and staged for commit -> Option A
Quick Check:
Submodule add = staged submodule [OK]
Hint: After adding, submodule shows staged in git status [OK]
Common Mistakes:
Expecting submodule folder as untracked
Thinking no changes appear after add
Assuming error if folder exists without checking
4. You ran git submodule add https://github.com/example/lib.git lib but got the error: fatal: destination path 'lib' already exists and is not an empty directory. What should you do to fix this?
medium
A. Use git submodule add --force to overwrite
B. Run git submodule update --init instead
C. Delete or move the existing 'lib' folder before adding the submodule
D. Rename the submodule URL to a different repository
Solution
Step 1: Understand the error cause
The error means the target folder 'lib' already exists and is not empty, so Git refuses to add the submodule there.
Step 2: Fix by clearing the folder
To add the submodule, you must delete or move the existing 'lib' folder so Git can create the submodule folder cleanly.
Final Answer:
Delete or move the existing 'lib' folder before adding the submodule -> Option C
Quick Check:
Remove existing folder to add submodule [OK]
Hint: Remove existing folder before adding submodule [OK]
Common Mistakes:
Trying to force add without clearing folder
Using update/init instead of add
Changing URL instead of fixing folder
5. You want to add a submodule but keep it in a different folder name than the repository's default. Which command correctly adds the submodule from https://github.com/example/utils.git into folder tools/utils?
hard
A. git submodule add https://github.com/example/utils.git tools/utils
B. git submodule add tools/utils https://github.com/example/utils.git
C. git submodule init https://github.com/example/utils.git tools/utils
D. git clone https://github.com/example/utils.git tools/utils
Solution
Step 1: Recall submodule add syntax
The syntax is git submodule add <url> [path], where path is the folder name you want.
Step 2: Apply to given URL and folder
git submodule add https://github.com/example/utils.git tools/utils correctly uses the URL first, then the custom folder path 'tools/utils'.
Final Answer:
git submodule add https://github.com/example/utils.git tools/utils -> Option A
Quick Check:
URL first, then folder path [OK]
Hint: URL first, then custom folder path in submodule add [OK]
Common Mistakes:
Swapping URL and folder arguments
Using init instead of add
Using clone instead of submodule add
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.