git submodule command

Git Basic Operations


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

git submoduleThe command is very useful for large projects or situations where 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.

Detailed usage

1. Initialize submodules

git submodule init

This command initializes all submodules in the configuration file. It sets the submodules' URLs and paths based on the information in the .gitmodules file, but it does not download the contents of the submodules.

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

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

2. Update submodule

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:Run this command after initializing the submodules, or when you need to update the submodule content.

git submodule update

3. Add submodule

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

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

<repo-url>is the repository address of the submodule,<path>Is the path of the submodule in the main repository (optional; if not specified, the name of the submodule repository is used as the path by default).

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

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

4. Remove submodule

git submodule deinit [<path>]
git rm [<path>]
  • git submodule deinit <path>: Remove submodule from.git/configremove it from the file, and delete the files in the submodule directory.
  • git rm <path>: Remove the submodule reference from the main repository and commit the changes.

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

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

Common usage:View 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 of submodules).
  • --remote: Pull the latest changes from the submodule's remote repository.

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

git submodule update --recursive --remote

7. Check submodule status

git submodule status

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

Common usage:View the current status of the submodules.

git submodule status

Git Basic Operations

other extensions