git submodule command
git submodule The command is used to manage projects that contain other Git repositories.
git submodule The command is very useful for large projects or situations where external libraries need to be integrated into a project. By using submodules, you can manage external libraries as part of your project without merging them directly into the main repository.
Usage details
** 1. Initialize submodules**
git submodule init
This command initializes all submodules in the configuration file. It sets the URL and path of the submodules based on the information in the .gitmodules file, but does not download the submodule content.
**Common usage:** After cloning a repository containing submodules, run this command to initialize the submodules.
git clone
cd
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 submodule content, run this command.
git submodule update
3. Add a submodule
git submodule add []
This command adds the specified Git repository as a submodule to the current repository.
is the repository address of the submodule, is the path of the submodule in the main repository (optional; if not specified, the submodule repository name is used as the path by default).
**Common usage:** Add an external library as a submodule to a project.
git submodule add https://github.com/example/libfoo.git libfoo
4. Remove a submodule
git submodule deinit []
git rm []
git submodule deinit: Remove the submodule from.git/configfile and delete the files in the submodule directory.git rm: Delete the submodule reference from the main repository and commit 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
List all submodules in the current repository, along with their commit hashes and paths.
**Common usage:** View the status of all submodules in a 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 submodules at all levels 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 submodules.
git submodule status
Git Basic Operations