Git & GitHub
/Advanced
Git Submodules
Definition
A feature that allows you to keep a Git repository as a subdirectory of another Git repository. This lets you clone another repository into your project and keep your commits separate.
Explain Like I'm New
A repo inside a repo. You are building an App. You want to use a specific Open Source Library, but you also want to be able to edit that library's code. You add the library as a Submodule. The App repo just saves a 'link' to the Library repo.
Real World Example
Using a shared 'UI Component Library' repository inside 5 different company projects. Instead of copying and pasting the CSS into all 5 projects, you add the UI repo as a submodule to all 5 projects.
Common Use Cases
- •Shared dependencies
- •Microservices architecture
Terminal Output
bash / terminal
# Add another repository as a folder inside your current project
$ git submodule add https://github.com/user/shared-ui-library.git
# This creates a special file called .gitmodules that tracks the link
# When your coworker clones the project, the folder will be empty!
# They MUST run this to download the submodule code:
$ git submodule update --init --recursive
Interview Questions
basic
- Does cloning a parent repository automatically download the contents of its submodules?
intermediate
- What happens if you run `git status` in the parent repo after changing a file inside the submodule?