Bird
Raised Fist0
Gitdevops~20 mins

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

Choose your learning style10 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
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
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
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
Hint

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

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

  1. Step 1: Understand what a submodule is

    A submodule is a way to include another Git repository inside your own project without merging histories.
  2. Step 2: Identify the purpose of adding a submodule

    It allows managing related projects separately but linked inside your main project.
  3. Final Answer:

    To include another Git repository inside your project as a separate component -> Option D
  4. 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

  1. Step 1: Recall the syntax for adding a submodule

    The correct syntax is git submodule add <url> [path].
  2. 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.
  3. Final Answer:

    git submodule add https://github.com/example/lib.git lib -> Option B
  4. 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

  1. 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.
  2. Step 2: Check git status behavior

    Git status will show the new submodule as added and staged for commit, not untracked or error.
  3. Final Answer:

    Shows new submodule 'tools' added and staged for commit -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    Delete or move the existing 'lib' folder before adding the submodule -> Option C
  4. 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

  1. Step 1: Recall submodule add syntax

    The syntax is git submodule add <url> [path], where path is the folder name you want.
  2. 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'.
  3. Final Answer:

    git submodule add https://github.com/example/utils.git tools/utils -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard