Bird
Raised Fist0
Gitdevops~7 mins

Submodules vs subtrees comparison in Git - CLI Comparison

Choose your learning style10 modes available
Introduction
Sometimes you want to include one project inside another. Git offers two ways: submodules and subtrees. Both help keep projects separate but linked, solving the problem of managing code dependencies across repositories.
When you want to include a library or tool from another repository inside your project without copying its code directly.
When you want to keep the included project’s history separate but still track its changes.
When you want to be able to update the included project independently from your main project.
When you want to avoid complex commands and prefer simpler workflows for including external code.
When you want to contribute back changes to the included project easily.
Commands
This command adds an external repository as a submodule inside your project at the path libs/lib. It links the external repo but keeps it separate.
Terminal
git submodule add https://github.com/example/lib.git libs/lib
Expected OutputExpected
Cloning into 'libs/lib'... remote: Enumerating objects: 100, done. remote: Counting objects: 100% (100/100), done. remote: Compressing objects: 100% (80/80), done. remote: Total 100 (delta 20), reused 50 (delta 10), pack-reused 0 Receiving objects: 100% (100/100), 10.00 KiB | 1.00 MiB/s, done. Resolving deltas: 100% (20/20), done.
This command initializes and fetches all submodules, making sure their content is downloaded and ready to use.
Terminal
git submodule update --init --recursive
Expected OutputExpected
Submodule 'libs/lib' (https://github.com/example/lib.git) registered for path 'libs/lib' Cloning into 'libs/lib'... remote: Enumerating objects: 100, done. remote: Counting objects: 100% (100/100), done. remote: Compressing objects: 100% (80/80), done. remote: Total 100 (delta 20), reused 50 (delta 10), pack-reused 0 Receiving objects: 100% (100/100), 10.00 KiB | 1.00 MiB/s, done. Resolving deltas: 100% (20/20), done.
→
--init - Initialize submodules that are not yet initialized
→
--recursive - Also initialize nested submodules inside submodules
This command adds the external repository as a subtree inside your project at libs/lib, copying its code and history into your project but keeping it merged.
Terminal
git subtree add --prefix=libs/lib https://github.com/example/lib.git main --squash
Expected OutputExpected
remote: Enumerating objects: 100, done. remote: Counting objects: 100% (100/100), done. remote: Compressing objects: 100% (80/80), done. remote: Total 100 (delta 20), reused 50 (delta 10), pack-reused 0 Receiving objects: 100% (100/100), 10.00 KiB | 1.00 MiB/s, done. Resolving deltas: 100% (20/20), done. From https://github.com/example/lib * branch main -> FETCH_HEAD Added dir 'libs/lib'
→
--prefix - Specifies the directory inside your project where the subtree will be placed
→
--squash - Combines all subtree commits into one to keep history simple
This command updates the subtree by pulling new changes from the external repository into your project.
Terminal
git subtree pull --prefix=libs/lib https://github.com/example/lib.git main --squash
Expected OutputExpected
remote: Enumerating objects: 10, done. remote: Counting objects: 100% (10/10), done. remote: Compressing objects: 100% (8/8), done. remote: Total 10 (delta 2), reused 5 (delta 1), pack-reused 0 Receiving objects: 100% (10/10), 1.00 KiB | 1.00 MiB/s, done. Resolving deltas: 100% (2/2), completed with 2 local objects. From https://github.com/example/lib * branch main -> FETCH_HEAD Squash commit -- not updating HEAD Updating 123abc..456def Fast-forward libs/lib/file.txt | 2 ++ 1 file changed, 2 insertions(+)
→
--prefix - Specifies the subtree directory to update
→
--squash - Keeps the update as a single commit
Key Concept

If you remember nothing else, remember: submodules keep projects separate and linked, requiring extra commands, while subtrees copy code inside your project for simpler use but merge histories.

Common Mistakes
Forgetting to run 'git submodule update --init' after cloning a repo with submodules
The submodule folders will be empty or missing, causing build or runtime errors
Always run 'git submodule update --init --recursive' after cloning to fetch submodule content
Trying to edit submodule files directly without committing in the submodule repo
Changes won't be tracked properly and can cause confusion or lost work
Enter the submodule directory, commit changes there, then update the main repo's submodule reference
Using subtree without the --prefix flag or with wrong directory path
The subtree code will be placed incorrectly, causing project structure issues
Always specify the correct --prefix path where you want the subtree code to live
Summary
Use 'git submodule add' to link an external repo inside your project without copying its code.
Run 'git submodule update --init --recursive' to fetch all submodule content after cloning.
Use 'git subtree add' with --prefix to copy external repo code inside your project with merged history.
Update subtrees with 'git subtree pull' to bring in changes from the external repo.
Submodules require extra commands to manage but keep histories separate; subtrees simplify usage by merging code.

Practice

(1/5)
1. What is the main difference between Git submodules and Git subtrees?
easy
A. Submodules and subtrees are identical in how they manage external repositories.
B. Submodules copy external repositories; subtrees link them separately.
C. Submodules merge external repositories automatically; subtrees require manual merging.
D. Submodules link external repositories separately; subtrees copy them into your project.

Solution

  1. Step 1: Understand submodules behavior

    Submodules keep external repositories linked but separate from the main project folder.
  2. Step 2: Understand subtrees behavior

    Subtrees copy the external repository content directly into your project folder, making it part of your codebase.
  3. Final Answer:

    Submodules link external repositories separately; subtrees copy them into your project. -> Option D
  4. Quick Check:

    Submodules = separate links, Subtrees = copied content [OK]
Hint: Remember: submodules link, subtrees copy [OK]
Common Mistakes:
  • Confusing which method copies code versus links it
  • Thinking submodules merge code automatically
  • Assuming subtrees keep repos separate
2. Which Git command correctly adds a submodule to your repository?
easy
A. git subtree add --prefix=folder_name repo_url branch
B. git submodule add repo_url folder_name
C. git add submodule repo_url folder_name
D. git clone --submodule repo_url folder_name

Solution

  1. Step 1: Identify the correct syntax for adding a submodule

    The correct command is 'git submodule add' followed by the repository URL and the folder name.
  2. Step 2: Verify other options

    git subtree add --prefix=folder_name repo_url branch is for subtrees, not submodules. git add submodule repo_url folder_name and git clone --submodule repo_url folder_name are invalid Git commands for adding submodules.
  3. Final Answer:

    git submodule add repo_url folder_name -> Option B
  4. Quick Check:

    Submodule add uses 'git submodule add' [OK]
Hint: Submodule add always starts with 'git submodule add' [OK]
Common Mistakes:
  • Using 'git subtree add' for submodules
  • Trying to add submodules with 'git add'
  • Confusing clone options with submodule commands
3. Given the commands below, what will be the result after cloning a repository with a submodule?
git clone https://example.com/main-repo.git
cd main-repo
git submodule update --init --recursive
medium
A. Only the main repository is cloned; submodules remain empty folders.
B. The submodules are cloned but not initialized.
C. The main repository and all submodules are cloned and initialized.
D. The clone fails because submodules require separate cloning.

Solution

  1. Step 1: Understand cloning with submodules

    Cloning the main repo alone does not fetch submodule content; submodules are empty initially.
  2. Step 2: Use 'git submodule update --init --recursive'

    This command fetches and initializes all submodules recursively, filling their folders with content.
  3. Final Answer:

    The main repository and all submodules are cloned and initialized. -> Option C
  4. Quick Check:

    Submodule update with init fetches submodules [OK]
Hint: Run 'git submodule update --init --recursive' after clone [OK]
Common Mistakes:
  • Assuming submodules clone automatically with main repo
  • Skipping the submodule update command
  • Confusing initialization with cloning
4. You added a subtree with git subtree add --prefix=lib https://example.com/lib.git main but later changes in the subtree are not reflected in your main repo. What is the likely cause?
medium
A. You forgot to pull subtree updates with git subtree pull.
B. Subtrees automatically sync changes; no action needed.
C. You must use git submodule update to sync subtrees.
D. Subtrees cannot be updated once added.

Solution

  1. Step 1: Understand subtree update process

    Subtrees copy code but do not auto-sync; you must manually pull updates.
  2. Step 2: Identify correct command to update subtree

    The command 'git subtree pull' fetches and merges changes from the external repo into your subtree folder.
  3. Final Answer:

    You forgot to pull subtree updates with git subtree pull. -> Option A
  4. Quick Check:

    Subtrees require manual pull to update [OK]
Hint: Use 'git subtree pull' to update subtree changes [OK]
Common Mistakes:
  • Expecting automatic subtree sync
  • Using submodule commands for subtrees
  • Thinking subtrees are read-only
5. You want to include a third-party library in your project. You want to keep it separate for easy updates but also want your team to clone the project with one command without extra steps. Which approach is best?
hard
A. Use Git subtrees to copy the library into your project for simpler cloning.
B. Use Git submodules and instruct the team to run extra commands after cloning.
C. Manually copy the library files without Git integration.
D. Use Git submodules but disable recursive cloning.

Solution

  1. Step 1: Analyze submodules pros and cons

    Submodules keep code separate but require extra commands after cloning to initialize submodules.
  2. Step 2: Analyze subtrees pros and cons

    Subtrees copy the library into the project, so cloning is simple with one command and no extra steps.
  3. Final Answer:

    Use Git subtrees to copy the library into your project for simpler cloning. -> Option A
  4. Quick Check:

    Subtrees simplify cloning by embedding code [OK]
Hint: Subtrees = easy clone; submodules = extra steps [OK]
Common Mistakes:
  • Choosing submodules despite extra clone steps
  • Ignoring subtree benefits for team convenience
  • Thinking manual copy is better than Git methods
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