Git submodules add external repositories as separate folders with a specific commit pointer, keeping their histories separate. Subtrees merge the external repository's history into the main repository, making it part of the main repo's commit history.
git clone https://example.com/project.git
git submodule update --init --recursive
The command git clone clones the main repository. The git submodule update --init --recursive command initializes and fetches all submodules recursively, ensuring their content is downloaded.
The git subtree pull command fetches and merges changes from the external repository into the specified subtree directory. The --prefix option specifies the subtree folder.
git submodule update --init, you notice the submodule is in a detached HEAD state. What is the best explanation?Git submodules track a specific commit, not a branch. When updated, they checkout that commit directly, resulting in a detached HEAD state. This is normal behavior.
Git submodules keep the shared library as a separate repository with its own history, allowing independent updates and avoiding merge conflicts in the main projects. Subtrees merge histories, which can complicate updates.