How to use the command 'git submodule' (with examples)
Git submodule is a command that allows you to inspect, update, and manage submodules within your Git repository. Submodules are Git repositories nested within another repository, and they allow you to include external code or libraries as dependencies in your project.
Use case 1: Install a repository’s specified submodules
Code:
git submodule update --init --recursive
Motivation:
When initializing a repository that contains submodules, you need to ensure that all the submodules are properly initialized and updated. Using the command git submodule update --init --recursive
allows you to initialize and update all the necessary submodules.
Explanation:
git submodule update
: This command updates the submodules in the repository. By default, it fetches the latest changes for the specified submodules.--init
: This option initializes any submodules that have not been initialized yet.--recursive
: This option initializes and updates all nested submodules.
Example output:
Submodule 'external-library' (https://github.com/example/external-library.git) registered for path 'external-library'
Submodule path 'external-library': checked out '0123456789abcdef0123456789abcdef01234567'
Use case 2: Add a Git repository as a submodule
Code:
git submodule add repository_url
Motivation:
If you want to include an external Git repository as a submodule in your project, the git submodule add
command allows you to easily add it.
Explanation:
git submodule add
: This command adds a new submodule to your repository.repository_url
: The URL of the Git repository you want to add as a submodule.
Example output:
Cloning into 'external-library'...
remote: Enumerating objects: 100, done.
remote: Counting objects: 100% (100/100), done.
remote: Compressing objects: 100% (80/80), done.
remote: Total 200 (delta 45), reused 50 (delta 20), pack-reused 100
Receiving objects: 100% (200/200), 25.00 KiB | 100.00 KiB/s, done.
Resolving deltas: 100% (85/85), done.
Submodule 'external-library' (https://github.com/example/external-library.git) registered for path 'external-library'
Use case 3: Add a Git repository as a submodule at the specified directory
Code:
git submodule add repository_url path/to/directory
Motivation:
In some cases, you may want to add a submodule to a specific directory within your repository. The git submodule add
command allows you to specify the desired directory path.
Explanation:
git submodule add
: This command adds a new submodule to your repository.repository_url
: The URL of the Git repository you want to add as a submodule.path/to/directory
: The directory path where you want to add the submodule. This path can be relative to your repository.
Example output:
Cloning into 'path/to/directory/external-library'...
remote: Enumerating objects: 100, done.
remote: Counting objects: 100% (100/100), done.
remote: Compressing objects: 100% (80/80), done.
remote: Total 200 (delta 45), reused 50 (delta 20), pack-reused 100
Receiving objects: 100% (200/200), 25.00 KiB | 100.00 KiB/s, done.
Resolving deltas: 100% (85/85), done.
Submodule 'external-library' (https://github.com/example/external-library.git) registered for path 'path/to/directory/external-library'
Use case 4: Update every submodule to its latest commit
Code:
git submodule foreach git pull
Motivation:
Keeping your submodules up to date is crucial to ensure that you are using the latest versions of the external code or libraries. The git submodule foreach git pull
command simplifies the process of updating all submodules.
Explanation:
git submodule foreach
: This command runs a specified command in each submodule.git pull
: This command updates the submodule to the latest commit.
Example output:
Entering 'external-library'
From https://github.com/example/external-library
0123456..abcdef7 main -> origin/main
Updating 0123456..abcdef7
Fast-forward
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Exiting 'external-library'