A Guide to Git Submodules: Managing Dependent Code in Projects
Git submodules are used to manage independent code repositories within the main project, avoiding the hassle of manual copying and updates. They are independent sub-repositories within the main project, where the main project only records the location and version of the submodules, while the submodules are maintained independently. Its core advantages include: independent development and testing, precise version control, and shared reuse across multiple projects. The usage steps are as follows: adding a submodule (`git submodule add`, which generates .gitmodules and configures and commits in the main project); cloning the main project requires `--recursive`, otherwise manually run `git submodule update`; updating submodules (`cd into the subdirectory and git pull` or `git submodule update` in the main project); deletion requires removing the directory and cleaning up configurations and caches. Note: After updating, the main project needs to commit version changes to avoid the "detached HEAD" state of submodules. Collaboration should follow the update-commit-merge process. Mastering these operations enables efficient management of project dependencies, reducing repetitive work and version confusion.
Read More