How to use git submodules to manage dependencies in a project?
Git submodules are a mechanism that allows you to include one repository inside another as a subdirectory. This is indispensable when your project depends on third-party code, such as a library or a shared utility, and you want to keep the ability to independently update this dependency while tracking a specific fixed version inside the main project.
To add a new submodule to your project, the `git submodule add` command is used, specifying the URL of the remote repository and the path where it should be placed. For example, the command `git submodule add https://github.com/example/lib.git libs/lib` will download the specified repository into the `libs/lib` folder and automatically create a `.gitmodules` file, which will store the configuration of the link between the repositories.
Cloning a project that contains submodules requires special attention, because by default, the submodule folders will be empty. To download the main project along with all dependencies, you need to use the `git clone --recurse-submodules` command. If the project has already been cloned with a regular command, submodules can be initialized and downloaded later using the `git submodule init` and `git submodule update` commands.
Updating submodules to the latest version requires moving inside the submodule folder and performing standard fetch operations, but most often developers manage them from the root of the main repository. It is important to remember that committing changes inside a submodule requires a separate commit within it, and the main project only stores a reference to a specific commit hash of that dependency.