What if you could include another project inside yours and keep it updated with just one command?
Why Adding a submodule in Git? - Purpose & Use Cases
Imagine you have a big project and want to include another project inside it, like adding a toolbox inside your main toolbox. You try copying all files manually every time the other project updates.
Manually copying files is slow and confusing. You might miss updates or overwrite changes. It's hard to keep track of which version you have, and sharing with teammates becomes a mess.
Adding a submodule lets you link another project inside yours cleanly. It keeps track of the exact version you use and updates easily without mixing files. It's like having a smart toolbox that knows when to refresh itself.
cp -r ../other-project ./my-project/other-project
# Repeat every time other-project changesgit submodule add https://github.com/user/other-project.git other-project
# One command to link and track updatesYou can manage multiple projects together smoothly, keeping each one updated and organized without extra manual work.
A web app uses a shared design library as a submodule. When the library updates, the app can easily pull the latest styles without copying files by hand.
Manual copying is slow and error-prone.
Submodules link projects cleanly and track versions.
They make updating and sharing easier and safer.
Practice
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 DQuick Check:
Submodule = separate included repo [OK]
- Thinking submodule merges histories
- Confusing submodule with backup
- Assuming submodule deletes files
https://github.com/example/lib.git into folder lib?Solution
Step 1: Recall the syntax for adding a submodule
The correct syntax isgit 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 BQuick Check:
Use 'git submodule add URL path' [OK]
- Using 'git add submodule' instead of 'git submodule add'
- Confusing 'init' with 'add'
- Trying to clone submodule like a normal repo
git submodule add https://github.com/example/tool.git tools, what will be the output of git status?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 AQuick Check:
Submodule add = staged submodule [OK]
- Expecting submodule folder as untracked
- Thinking no changes appear after add
- Assuming error if folder exists without checking
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?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 CQuick Check:
Remove existing folder to add submodule [OK]
- Trying to force add without clearing folder
- Using update/init instead of add
- Changing URL instead of fixing folder
https://github.com/example/utils.git into folder tools/utils?Solution
Step 1: Recall submodule add syntax
The syntax isgit 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 AQuick Check:
URL first, then folder path [OK]
- Swapping URL and folder arguments
- Using init instead of add
- Using clone instead of submodule add
Ready to prove it?
Pass the test and this pattern moves unknown
