https://www.geeksforgeeks.org/git/how-to-use-the-git-submodule-init-and-update-command/
— main source
Let's discuss the most common scenario first: You are being told to clone a repo that is using git submodules:
$ git clone --recurse-submodules https://github.com/username/main-repo main-repo.git
--recurse-submodules includes the following commands (see further below for individual explanations):
$ git clone https://github.com/username/main-repo main-repo.git
$ cd main-repo.git
$ git submodule init
$ git submodule update
add a submodule
This adds the submodule information to .gitmodules:
$ git submodule add <repository-url> <path>
init a submodule
This copies the mapping from the .gitmodules file into the local git configuration file .git/config:
$ git submodule init
update a submodule
This updates the submodule to the commit specified in the main project's repository,clones the submodule repository into the specified path and checks out the correct commit:
$ git submodule update
init and update together
$ git submodule update --init
update a submodule from remote
This fetches the latest changes from the submodule's remote repository and checks out the latest commit:
$ git submodule update --remote
working with submodules
# Imagine './libs/library' being our git submodule.
$ cd libs/library
# Make changes to the submodule.
# Update the submodule.
$ git add .
$ git commit -m "Update library."
$ git push
# Update the main project, reference to submodule.
$ cd ../../
$ git add libs/library
$ git commit -m "Update submodule library to latest commit."
$ git push
remove a submodule
# deinitialize the submodule
$ git submodule deinit -f -- libs/library
# remove the submodule's directory
$ rm -rf libs/library
# remove the submodule's reference
$ git add .gitmodules
$ git rm --cached libs/library
$ git commit -m "Remove submodule libs/library."
$ git push