Git Submodules: The Proper Way to Incorporate Third-Party Code into Your Project
Git submodules are used to address issues such as version control loss, collaborative chaos, and code redundancy when a parent project reuses third-party code. The core idea is to embed an independent sub-repository within the parent project, which only records the location and version information of the submodule, facilitating independent tracking of updates. Basic usage: After initializing the parent project, use `git submodule add` to add a third-party repository as a submodule (generating the `.gitmodules` file to record configurations). When cloning a parent project containing submodules, use `--recursive` or manually execute `git submodule update` to pull the submodule code. Submodules can be modified and updated independently, while the parent project needs to commit new references to the submodule to synchronize versions. Note: Submodules do not update automatically; you need to manually enter the submodule directory, execute `git pull`, and then commit the parent project. For multi-person collaboration, `.gitmodules` and submodule versions must be shared to ensure consistent paths and versions. Submodules differ from subtree merging, where the former is maintained independently, while the latter merges code into the parent project.
Read More