HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

An introductory guide to Git submodules

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
introductorysubmodulesguidegit

Problem

Git submodules are a powerful feature that allow you to include other repositories within your own repository. This can be useful for including libraries or other code that you want to keep separate from your main repository.
To add a submodule to your repository, you can use git submodule add <upstream-path> <local-path>. The first argument is the URL of the repository you want to add as a submodule, and the second argument is the path where you want the submodule to be located within your repository.
Adding a submodule will create a new directory in your repository that contains the contents of the submodule repository. The submodule directory will contain a .git directory that points to the submodule repository, allowing you to track changes to the submodule independently of your main repository.
Additionally, a .gitmodules file will be created in the root of your repository that contains information about the submodule, such as the URL of the submodule repository and the path where the submodule is located.
After adding a submodule to your repository, you will need to initialize and update the submodule to fetch its contents. You can do this by running git submodule update --init --recursive.

Solution

# Syntax: git submodule add <upstream-path> <local-path>

git submodule add https://github.com/30-seconds/30-seconds-of-code ./30code
# Creates the directory `30code` containing the submodule from
# "https://github.com/30-seconds/30-seconds-of-code"


Adding a submodule will create a new directory in your repository that contains the contents of the submodule repository. The submodule directory will contain a .git directory that points to the submodule repository, allowing you to track changes to the submodule independently of your main repository.
Additionally, a .gitmodules file will be created in the root of your repository that contains information about the submodule, such as the URL of the submodule repository and the path where the submodule is located.
After adding a submodule to your repository, you will need to initialize and update the submodule to fetch its contents. You can do this by running git submodule update --init --recursive.
While the --recursive flag isn't strictly necessary, it ensures that any nested submodules are also updated. This is useful if the submodule itself contains submodules, as it will ensure that all submodules are initialized and updated.
When you clone a repository that contains submodules, the submodules will be checked out at a specific commit. When you want to update the contents of the submodules to the latest version, you can use git submodule update --recursive --remote. Again, the --recursive flag ensures that any submodules within the submodule are also updated.
Luckily, renaming a submodule is just as easy as renaming a directory. Using git mv <old-submodule> <new-submodule> will rename the directory containing the submodule. This will also update the submodule's path in the .gitmodules file.

Code Snippets

# Syntax: git submodule add <upstream-path> <local-path>

git submodule add https://github.com/30-seconds/30-seconds-of-code ./30code
# Creates the directory `30code` containing the submodule from
# "https://github.com/30-seconds/30-seconds-of-code"
# Syntax: git submodule update --init --recursive

git submodule update --init --recursive
# Clones missing submodules and checks out commits
# Syntax: git submodule update --recursive --remote

git submodule update --recursive --remote
# Pulls all submodules from their respective remotes

Context

From 30-seconds-of-code: submodules

Revisions (0)

No revisions yet.