what is git submodule

1 year ago 68
Nature

A Git submodule is a record within a host Git repository that points to a specific commit in another external repository. Submodules allow you to keep a Git repository as a subdirectory of another Git repository, which can be very useful when you want to incorporate and track version history of external code. When adding a submodule to a repository, a new .gitmodules file will be created, which contains metadata about the mapping between the submodule projects URL and local directory.

Some key points to keep in mind about Git submodules include:

  • Submodules are very static and only track specific commits. They do not track Git refs or branches and are not automatically updated when the host repository is updated.
  • Submodules are simply a reference to another repository at a particular snapshot in time.
  • Submodules do not add the code of the submodule to the main repository, but only add information about the submodule that is added to the main repository.
  • Git submodule add is used to add a new submodule to an existing repository.
  • Git submodule update is used to update the submodules to the latest commit.
  • Git submodule init is used to initialize the submodules.
  • Git submodule update --init --recursive is used to put the submodules in the right state.
  • Git checkout --recurse-submodules is used to checkout the submodules.
  • Git submodule update --remote is used to update the submodules to the latest commit on the remote branch.

Git submodules are useful for managing dependencies in an elegant, robust way, and provide a consistent, reliable interface. They are simply standard Git repositories that are placed inside another, parent Git repository.