Sub topic 3
Adding a submodule
This chapter assumes you are comfortable with everyday Git (commit, branch, push, pull). It also assumes you know that a commit hash identifies one exact snapshot of a repository. Earlier chapters covered why you might embed another repository and how gitlinks and .gitmodules work. Here you add a submodule and read the result.
The command
Run git submodule add from the root of the parent repository. It takes the URL of the repository to embed and the path where it should live.
git submodule add https://github.com/example/libfoo.git vendor/libfoo
git status
git commit -m "Add libfoo as a submodule"The command does four things:
- It clones
libfoointovendor/libfooand checks out the default branch of that repository. - It creates
.gitmodulesin the parent repository, or appends to it if it already exists. - It stores the submodule's own Git data under
.git/modules/vendor/libfooin the parent. Thevendor/libfoodirectory holds a small.gitfile that points there. - It stages two changes: the
.gitmodulesfile and a gitlink entry forvendor/libfoo.
The path argument is optional. If you leave it out, Git uses the repository name from the URL, so the clone lands in libfoo at the top level. Pass an explicit path when you want the code under a directory such as vendor/.
What `git status` shows
After the command, git status reports two new files to be committed:
Changes to be committed:
new file: .gitmodules
new file: vendor/libfooThe second line is the gitlink. It is not a directory of files from the parent's point of view. It is a single entry in the parent's tree that records one commit hash of libfoo. The parent never stores libfoo's files or history itself.
The `.gitmodules` file
Git writes this file for you:
[submodule "vendor/libfoo"]
path = vendor/libfoo
url = https://github.com/example/libfoo.gitIt maps the submodule's path to the URL used to clone it. It is a normal tracked file, so teammates receive it when they pull. It does not say which commit to use. That information is in the gitlink.
Choosing a branch
By default the submodule is checked out at the remote's default branch, and the parent pins whatever commit that branch pointed to at that moment. To start from a different branch, use -b:
git submodule add -b stable https://github.com/example/libfoo.git vendor/libfooThis adds a branch = stable line to the .gitmodules entry. The line only tells git submodule update --remote which branch to follow later. The parent still pins one exact commit, and it does not follow the branch automatically.
Commit both pieces
You must commit the .gitmodules file and the gitlink together. If you commit only one, a teammate who pulls gets a gitlink with no URL, or a URL with no pinned commit, and cannot initialize the submodule. The commit in the first example stages both because git submodule add already staged them.
Common errors
- "already exists in the index": the path is already tracked by the parent, often because the files were committed earlier. Remove them from the index with
git rm -r --cached vendor/libfoo, then run the add command again. - "already exists and is not a valid git repo": the target directory exists and is non-empty. Move or delete it first.
- Protocol errors on local paths: newer Git versions block the
filetransport inside submodules by default for security. Prefer a network URL. - Forgetting to commit
.gitmodules: the add command stages it, but if you later edit it by hand, stage that change too.
Checking the result
Two commands confirm that the submodule is registered correctly:
git submodule status
git diff --cached --submodulegit submodule status prints the pinned commit hash and the path. git diff --cached --submodule shows the staged change as a commit range summary, rather than a raw hash change, which is easier to read.
Exercise
You ran git submodule add and then committed only the gitlink, using git commit vendor/libfoo. A teammate pulls your commit and cannot initialize the submodule. What is missing, and why does it break?
Show answer
The .gitmodules file was not committed. The gitlink records only a commit hash, so Git on the teammate's machine has no URL to clone from. Commit .gitmodules together with the gitlink.
Exercise
After adding a submodule with -b stable, which commit does the parent repository record, and what does the branch setting affect?
Show answer
The parent records the single commit that stable pointed to at the time of the add. The branch setting in .gitmodules only tells git submodule update --remote which branch to fetch later.