git submodule manages dependency libraries

This article is transcoded by 简悦 SimpRead, original source www.runoob.com Git Create Repository Git Branch Management

The git submodule command is used to manage projects that contain other Git repositories.

The git submodule command is very useful for large projects or when you need to integrate external libraries into your project. By using submodules, you can manage external libraries as part of your project without having to merge them directly into the main repository.

Usage Details

1. Initialize Submodules

git submodule init

This command initializes all the submodules in the configuration file. It sets the submodule URLs and paths according to the information in the .gitmodules file but does not download the submodule content.

Common usage: After cloning a repository with submodules, run this command to initialize the submodules.

git clone <repo-url>
cd <repo-dir>
git submodule init

2. Update Submodules

git submodule update

This command pulls the submodule content from the submodule’s remote repository and updates it to the commit specified in the .gitmodules file.

Common usage: After initializing submodules or when you need to update the content of submodules, run this command.

git submodule update

3. Add Submodules

git submodule add <repo-url> [<path>]

This command adds the specified Git repository as a submodule to the current repository.

<repo-url> is the address of the submodule repository, and <path> is the submodule’s path within the main repository (optional; if not provided, the submodule repository’s name is used as the path by default).

Common usage: Add an external library as a submodule to your project.

git submodule add https://github.com/example/libfoo.git libfoo

4. Remove Submodules

git submodule deinit [<path>]
git rm [<path>]
  • git submodule deinit <path>: Removes the submodule from the .git/config file and deletes files in the submodule directory.
  • git rm <path>: Removes the submodule reference from the main repository and commits the change.

Common usage: Remove a submodule from the main repository.

git submodule deinit libfoo
git rm libfoo
rm -rf .git/modules/libfoo

5. List Submodules

git submodule

Lists all submodules in the current repository along with their commit hashes and paths.

Common usage: Check the status of all submodules in the project.

git submodule

6. Update All Submodules

git submodule update --recursive --remote
  • --recursive: Recursively update all submodules (including submodules within submodules).
  • --remote: Pulls the latest changes from the submodule’s remote repository.

Common usage: When submodules contain other submodules, ensure all levels of submodules are updated to the latest versions.

git submodule update --recursive --remote

7. Check Submodule Status

git submodule status

Displays the current status of submodules, including the current commit hash, path, and whether there are uncommitted changes.

Common usage: Check the current status of submodules.

git submodule status