0
0
Gitdevops~30 mins

Submodules vs subtrees comparison in Git - Hands-On Comparison

Choose your learning style9 modes available
Compare Git Submodules and Subtrees
📖 Scenario: You are managing a main project that needs to include code from another project. You want to learn two ways to include that code: using Git submodules and Git subtrees. This will help you decide which method to use in your projects.
🎯 Goal: Learn how to add a Git submodule and a Git subtree to a main repository, and understand the differences between them by practicing basic commands.
📋 What You'll Learn
Have Git installed and configured
Create and use local Git repositories
Understand basic Git commands like clone, add, commit, push, and pull
💡 Why This Matters
🌍 Real World
Developers often need to include code from other projects. Git submodules and subtrees are two ways to do this, each with pros and cons.
💼 Career
Understanding submodules and subtrees helps in managing dependencies and collaborating across multiple repositories in software development jobs.
Progress0 / 4 steps
1
Create the main repository and the external repository
Create two local Git repositories: one called main-project and another called external-lib. Initialize both repositories with git init. In external-lib, create a file named lib.txt with the content External library code, then add and commit it.
Git
Need a hint?

Use mkdir to create folders, git init to initialize repos, and echo to create the file.

2
Add the external repository as a Git submodule
Inside the main-project directory, add the external-lib repository as a Git submodule using the relative path ../external-lib. Then commit the change with the message Added external-lib as submodule.
Git
Need a hint?

Use git submodule add with the relative path and folder name, then commit with git commit -am.

3
Add the external repository as a Git subtree
Inside the main-project directory, add the external-lib repository as a Git subtree under the folder external-lib-subtree using the relative path ../external-lib and prefix external-lib-subtree/. Then commit the change with the message Added external-lib as subtree.
Git
Need a hint?

Use git remote add to add the external repo, then git subtree add --prefix= to add it as subtree, and commit.

4
Display the folder structure to compare submodule and subtree
From the main-project directory, list the contents of the folders external-lib and external-lib-subtree to show the files included by submodule and subtree.
Git
Need a hint?

Use ls external-lib and ls external-lib-subtree to see the files inside both folders.