Sub topic 2
How submodules work: gitlinks and .gitmodules
This chapter assumes you are comfortable with everyday Git (commit, branch, merge, push, pull). It also assumes you know that a commit hash identifies one exact snapshot of a repository, and that you can use a shell. You do not need to have used submodules before. Here you build the mental model that explains every submodule command and most submodule surprises.
The core idea: a pointer, not a copy
A submodule is a separate Git repository that sits in a subdirectory of another repository. The outer repository is the superproject, or parent. The inner one is the submodule.
The parent does not store the submodule's files or history. It stores two small facts:
- Which commit of the submodule it expects. This is recorded in the parent's commit history as a special tree entry called a gitlink.
- Where to get that repository. This is recorded in a plain-text file called
.gitmodules.
Everything else, such as the submodule's files, branches, and history, lives in the submodule's own repository. This is why the parent stays small and why you must treat the two repositories as separate when you commit and push.
The gitlink: a tree entry that holds a commit hash
Git trees normally contain two kinds of entries: blobs (file contents) and other trees (directories). A gitlink is a third kind. Its mode is 160000, and its object ID is a commit hash from a different repository.
You can see it with git ls-tree:
git ls-tree HEAD100644 blob 4b825dc642cb6eb9a060e54bf8d69288fbee4904 .gitmodules
100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 README.md
040000 tree 9c1d2e3f4a5b6c7d8e9f00112233445566778899 src
160000 commit a1b2c3d4e5f60718293a4b5c6d7e8f9012345678 vendor/libfooThe last line is the gitlink. The type is commit, which a normal tree never contains, and the mode is 160000. The hash a1b2c3d4... does not exist in the parent's object database. Git does not try to find it there. It only checks that object when you go inside the submodule.
Two consequences follow from this design.
- The version is pinned by the parent's history. Checking out an old parent commit means the gitlink at that point names the old submodule commit. The parent therefore knows which submodule version each of its own commits was built against.
- Moving the submodule is a change to the parent. If the submodule's checked-out commit changes, the parent sees a modified path. You must commit that change in the parent to record it.
How the parent shows a changed pointer
When the submodule is on a different commit than the parent recorded, git diff in the parent shows two "Subproject commit" lines:
diff --git a/vendor/libfoo b/vendor/libfoo
index a1b2c3d..9f8e7d6 160000
--- a/vendor/libfoo
+++ b/vendor/libfoo
@@ -1 +1 @@
-Subproject commit a1b2c3d4e5f60718293a4b5c6d7e8f9012345678
+Subproject commit 9f8e7d6c5b4a39281706f5e4d3c2b1a098765432That is the whole diff. Git shows no file changes, because the parent does not know about them. For a readable summary, ask Git to look inside the submodule:
git diff --submodule=logThis lists the submodule commits between the old and new pointer, which is far more useful in code review.
.gitmodules: where the submodule comes from
.gitmodules is an ordinary text file at the top of the working tree. It is committed like any other file, so everyone who clones the parent gets the same mapping. It uses Git's config-file syntax:
[submodule "vendor/libfoo"]
path = vendor/libfoo
url = https://github.com/example/libfoo.gitEach section name, such as "vendor/libfoo", is the submodule's name. By default the name equals the path, but the two are separate settings. Renaming the directory later changes path and leaves the name alone.
The file can also hold optional settings:
| Key | Meaning |
|---|---|
| `path` | Directory in the parent where the submodule is checked out. |
| `url` | Where to clone from. May be absolute or relative to the parent's own remote URL. |
| `branch` | Branch that `git submodule update --remote` follows. Without it, the remote's default branch (`HEAD`) is used. |
| `update` | Strategy for `git submodule update`, such as `checkout` (the default), `rebase`, or `merge`. |
| `shallow` | If `true`, clone the submodule with depth 1. |
| `ignore` | Controls what `git status` and `git diff` report for this submodule. |
Note what .gitmodules does not contain: the commit. The file says where the code lives, and the gitlink says which version of it to use. Changing the URL in .gitmodules does not change the version, and changing the version does not change the URL.
A relative URL such as url = ../libfoo.git resolves against the parent's remote URL. This keeps the parent and its submodules together when a company moves hosts or switches between HTTPS and SSH.
Three places hold submodule state
A common source of confusion is that submodule information lives in three places, and they are not always in sync.
| Location | Committed? | Holds |
|---|---|---|
| Parent's tree (gitlink) | Yes | The exact commit the parent expects. |
| `.gitmodules` | Yes | Name, path, and default URL for each submodule. |
| `.git/config` in the parent | No, local only | Which submodules this clone has initialized, and the URL actually used. |
When you clone a parent, .gitmodules arrives with the files, but nothing is registered locally yet. Initializing a submodule copies its URL from .gitmodules into your local .git/config:
[submodule "vendor/libfoo"]
active = true
url = https://github.com/example/libfoo.gitLater commands read the local copy. This lets you override a URL on your machine, for example to point at a mirror or a fork, without touching the committed file. It also explains why editing .gitmodules after a clone appears to do nothing. The local entry wins until you re-sync it with git submodule sync.
What the submodule looks like on disk
Inside vendor/libfoo you will find a file named .git, not a directory:
cat vendor/libfoo/.gitgitdir: ../../.git/modules/vendor/libfooModern Git keeps each submodule's real repository data under the parent's .git/modules/ directory. The .git file in the submodule's working tree is a pointer to it. This has practical effects:
- Deleting the working directory
vendor/libfoodoes not delete the cloned history. It remains in.git/modules/vendor/libfoo, which is why a later checkout can restore it without downloading again. - Running
gitinsidevendor/libfoobehaves as if you were in a normal, independent repository. It has its own branches, remotes, index, andHEAD. - When you run
gitin the parent, Git treatsvendor/libfooas one opaque entry and does not descend into it.
The practical rule is that the directory you are standing in decides which repository you are talking to.
Detached HEAD is the normal state
After a parent checks out or updates a submodule, the submodule is on a detached HEAD. Git has checked out the exact commit hash named by the gitlink, not a branch. Look inside to confirm:
cd vendor/libfoo
git statusHEAD detached at a1b2c3d
nothing to commit, working tree cleanThis is expected, because the parent pinned a commit and no branch is involved. The consequence is that commits made while detached belong to no branch. If you switch away without creating a branch, they become hard to find. A later chapter covers the correct workflow for editing inside a submodule. For now, remember to create a branch before you commit in a submodule.
Inspecting state with git submodule status
The quickest way to compare the three places is:
git submodule status a1b2c3d4e5f60718293a4b5c6d7e8f9012345678 vendor/libfoo (v1.2.0)The first character of each line is a status flag:
| Flag | Meaning |
|---|---|
| (space) | The checked-out commit matches the gitlink. |
| `-` | The submodule is not initialized. |
| `+` | The checked-out commit differs from the gitlink in the parent. |
| `U` | The submodule has merge conflicts. |
A + means the parent's recorded pointer and the submodule's actual checkout disagree. Either you moved the submodule and have not committed the new pointer, or you pulled a parent change and have not yet updated the submodule. Which one it is determines the fix, and that is the most useful habit to build from this chapter.
Edge cases that follow from the model
- Pulling the parent does not move the submodule. A pull changes the gitlink in the parent, but the submodule's working tree stays on the old commit until you update it. You then see
+in status and a modified path ingit status. Committing that state would silently revert the pointer. - A pointer can reference a commit nobody can fetch. If someone commits a new pointer in the parent but never pushes the commit inside the submodule, other developers get an error saying Git could not find that commit. The gitlink only holds a hash, and Git cannot create the object from it.
- The submodule has its own uncommitted changes. The parent reports the path as modified, and
git diffshows-dirtyon the new hash. These changes are not part of any parent commit. - Branch switching in the parent can leave stale directories. If a branch has a submodule that another branch lacks, switching can leave the old directory behind as untracked content.
- The URL can be unreachable. The pointer is meaningless to a clone that cannot reach the URL in
.gitmodules. A private submodule requires every user and CI system to have credentials for it.
Confirming the model yourself
You can look at the raw data without any special commands. This sequence prints the gitlink hash, then checks that the same hash is the submodule's HEAD:
# The commit the parent expects
git ls-tree HEAD vendor/libfoo
# The commit actually checked out inside the submodule
git -C vendor/libfoo rev-parse HEAD
# The URL committed in .gitmodules, and the URL used locally
git config -f .gitmodules --get submodule.vendor/libfoo.url
git config --get submodule.vendor/libfoo.urlIf the first two hashes match, git submodule status shows a space. If they differ, it shows +. If the two URLs differ, you have a local override, and git submodule sync will replace the local value with the committed one.
Exercise
A teammate pulls the latest parent branch. git status in the parent now shows modified: vendor/libfoo (new commits), although the teammate edited nothing, and git submodule status prints a line starting with +. What happened, which repository holds the "wrong" state, and which command makes the submodule match what the parent expects?
Show answer
The pull changed the gitlink in the parent to a newer commit hash, but a pull does not move the submodule's checkout. The submodule's working tree is still on the old commit, so it differs from the pointer, which is what + reports. The parent's pointer is the intended state, and the submodule checkout is stale. Running git submodule update --init --recursive checks out the commit named by the gitlink. The teammate should not commit the stale state, because that would revert the pointer to the old version.
Exercise
Name the three places submodule information is stored, say which of them are committed, and explain why changing url in .gitmodules after cloning may seem to have no effect.
Show answer
The three places are the gitlink in the parent's tree (committed, holds the commit hash), the .gitmodules file (committed, holds name, path, URL, and optional settings), and .git/config in the parent (local only, holds the URL used by this clone after initialization). Commands read the local .git/config entry once a submodule is initialized, so a later edit to .gitmodules is ignored until git submodule sync copies the new URL into the local config.