GITSUBMODULES(7) | Git Manual | GITSUBMODULES(7) |
gitsubmodules - Mounting one repository inside another
.gitmodules, $GIT_DIR/config
git submodule git <command> --recurse-submodules
A submodule is a repository embedded inside another repository. The submodule has its own history; the repository it is embedded in is called a superproject.
On the filesystem, a submodule usually (but not always - see FORMS below) consists of (i) a Git directory located under the $GIT_DIR/modules/ directory of its superproject, (ii) a working directory inside the superproject’s working directory, and a .git file at the root of the submodule’s working directory pointing to (i).
Assuming the submodule has a Git directory at $GIT_DIR/modules/foo/ and a working directory at path/to/bar/, the superproject tracks the submodule via a gitlink entry in the tree at path/to/bar and an entry in its .gitmodules file (see gitmodules(5)) of the form submodule.foo.path = path/to/bar.
The gitlink entry contains the object name of the commit that the superproject expects the submodule’s working directory to be at.
The section submodule.foo.* in the .gitmodules file gives additional hints to Git’s porcelain layer. For example, the submodule.foo.url setting specifies where to obtain the submodule.
Submodules can be used for at least two different use cases:
Submodule operations can be configured using the following mechanisms (from highest to lowest precedence):
For example an effect from the submodule’s .gitignore file would be observed when you run git status --ignore-submodules=none in the superproject. This collects information from the submodule’s working directory by running status in the submodule while paying attention to the .gitignore file of the submodule.
The submodule’s $GIT_DIR/config file would come into play when running git push --recurse-submodules=check in the superproject, as this would check if the submodule has any changes not published to any remote. The remotes are configured in the submodule as usual in the $GIT_DIR/config file.
If the submodule is not yet initialized, then the configuration inside the submodule does not exist yet, so where to obtain the submodule from is configured here for example.
This file mainly serves as the mapping between the name and path of submodules in the superproject, such that the submodule’s Git directory can be located.
If the submodule has never been initialized, this is the only place where submodule configuration is found. It serves as the last fallback to specify where to obtain the submodule from.
Submodules can take the following forms:
It is possible to construct these old form repositories manually.
When deinitialized or deleted (see below), the submodule’s Git directory is automatically moved to $GIT_DIR/modules/<name>/ of the superproject.
A submodule can be deinitialized by running git submodule deinit. Besides emptying the working directory, this command only modifies the superproject’s $GIT_DIR/config file, so the superproject’s history is not affected. This can be undone using git submodule init.
The deletion removes the superproject’s tracking data, which are both the gitlink entry and the section in the .gitmodules file. The submodule’s working directory is removed from the file system, but the Git directory is kept around as it to make it possible to checkout past commits without requiring fetching from another repository.
To completely remove a submodule, manually delete $GIT_DIR/modules/<name>/.
A submodule is considered active,
or
or
and these are evaluated in this order.
For example:
[submodule "foo"]
active = false
url = https://example.org/foo [submodule "bar"]
active = true
url = https://example.org/bar [submodule "baz"]
url = https://example.org/baz
In the above config only the submodule bar and baz are active, bar due to (1) and baz due to (3). foo is inactive because (1) takes precedence over (3)
Note that (3) is a historical artefact and will be ignored if the (1) and (2) specify that the submodule is not active. In other words, if we have a submodule.<name>.active set to false or if the submodule’s path is excluded in the pathspec in submodule.active, the url doesn’t matter whether it is present or not. This is illustrated in the example that follows.
[submodule "foo"]
active = true
url = https://example.org/foo [submodule "bar"]
url = https://example.org/bar [submodule "baz"]
url = https://example.org/baz [submodule "bob"]
ignore = true [submodule]
active = b*
active = :(exclude) baz
In here all submodules except baz (foo, bar, bob) are active. foo due to its own active flag and all the others due to the submodule active pathspec, which specifies that any submodule starting with b except baz are also active, regardless of the presence of the .url field.
# Add a submodule git submodule add <URL> <path>
# Occasionally update the submodule to a new version: git -C <path> checkout <new version> git add <path> git commit -m "update submodule to new version"
# See the list of submodules in a superproject git submodule status
# See FORMS on removing submodules
# Enable recursion for relevant commands, such that # regular commands recurse into submodules by default git config --global submodule.recurse true
# Unlike most other commands below, clone still needs # its own recurse flag: git clone --recurse <URL> <directory> cd <directory>
# Get to know the code: git grep foo git ls-files --recurse-submodules
git ls-files also requires its own --recurse-submodules flag.
# Get new code git fetch git pull --rebase
# Change worktree git checkout git reset
When cloning or pulling a repository containing submodules the submodules will not be checked out by default; you can instruct clone to recurse into submodules. The init and update subcommands of git submodule will maintain submodules checked out and at an appropriate revision in your working tree. Alternatively you can set submodule.recurse to have checkout recursing into submodules (note that submodule.recurse also affects other Git commands, see git-config(1) for a complete list).
git-submodule(1), gitmodules(5).
Part of the git(1) suite
06/06/2021 | Git 2.32.0 |