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. Here you will learn what Git stores when one repository contains another. The commands for adding and cloning come in the next chapters.
The core idea: a pointer, not a copy
A submodule is a separate repository checked out inside a directory of the parent repository. The parent does not store the submodule's files or history. It stores one commit hash from the submodule's repository. Anyone who checks out a given parent commit therefore gets the same submodule commit, however far the upstream project has moved on.
Two pieces of data make this work:
- The gitlink is the pinned commit hash. It lives in the parent's tree.
- The
.gitmodulesfile maps the submodule's path to the URL it is cloned from. It is a normal tracked file.
The gitlink
A normal tree entry is a blob (a file) or a tree (a directory). A gitlink is a third kind: an entry with mode 160000 whose object is a commit from another repository. You can see it with git ls-tree:
git ls-tree HEAD vendor/160000 commit 3f2a9c1d8e7b6a5f4c3b2a1908d7e6f5a4b3c2d1 vendor/libfooThe parent repository does not contain that commit object. Git does not try to resolve it in the parent's object database. It is only a recorded hash that the submodule's own repository must be able to supply.
Because the gitlink lives in the tree, the pin is versioned like any other content. Updating a submodule means committing a new hash in the parent, and git log and git diff show that change.
.gitmodules
The gitlink holds the commit but not the location. The location is in .gitmodules, at the top of the parent's working tree:
[submodule "vendor/libfoo"]
path = vendor/libfoo
url = https://github.com/example/libfoo.gitThe name in quotes identifies the submodule. It is usually the same as the path, but it stays fixed if the directory is later moved. Since the file is tracked, everyone who clones the parent receives the same mapping. You can read it with Git's config tooling:
git config -f .gitmodules --get-regexp '^submodule\..*\.url$'Local state: .git/config and .git/modules
.gitmodules is shared. Each clone also keeps private state about the submodule.
- After initialization, the URL is copied into the parent's
.git/config. Git reads the URL from there afterwards, so you can point your own clone at a mirror without changing the shared file. - The submodule's actual repository data lives in
.git/modules/vendor/libfooinside the parent's.gitdirectory. - The submodule directory itself contains a small
.gitfile, not a directory. It holds one line:gitdir: ../../.git/modules/vendor/libfoo.
Storing the data under the parent's .git is why you can delete a submodule's working directory without losing its history. It is also why a complete removal needs an extra cleanup step, covered in a later chapter.
Reading the state
git submodule status compares the pinned hash with what is checked out:
git submodule status 3f2a9c1d8e7b6a5f4c3b2a1908d7e6f5a4b3c2d1 vendor/libfoo (v1.4.0)The first character matters:
| Prefix | Meaning |
|---|---|
| (space) | Checked-out commit matches the gitlink |
| `-` | Submodule is not initialized |
| `+` | Checked-out commit differs from the gitlink |
| `U` | The submodule has merge conflicts |
Common errors that follow from the model
- Empty directory after cloning. The gitlink is present, but the submodule's data has not been fetched. The parent only records the pin.
+in the status after a pull. Someone moved the pin and your checkout has not followed yet.- "Did not match any file(s)" or "not our ref" on update. The pinned commit was never pushed to the submodule's remote, so Git cannot fetch it.
- Changing files inside the submodule and seeing
modifiedin the parent. The parent notices that the submodule has new commits or uncommitted changes. It tracks only the pinned hash, and the pin does not change until you commit it.
Exercise
A teammate clones your parent repository. git ls-tree HEAD shows a mode 160000 entry for vendor/libfoo, but the vendor/libfoo directory is empty. Which two pieces of information does Git have about the submodule at this point, and where is each stored?
Show answer
The commit hash to use is stored as the gitlink, the mode 160000 entry in the parent's tree. The URL to fetch it from is stored in the tracked .gitmodules file. The submodule's own repository data has not been fetched yet, so the directory is empty.