1GITSUBMODULES(7) Git Manual GITSUBMODULES(7)
2
3
4
6 gitsubmodules - mounting one repository inside another
7
9 .gitmodules, $GIT_DIR/config
10
11 git submodule
12 git <command> --recurse-submodules
13
14
16 A submodule is a repository embedded inside another repository. The
17 submodule has its own history; the repository it is embedded in is
18 called a superproject.
19
20 On the filesystem, a submodule usually (but not always - see FORMS
21 below) consists of (i) a Git directory located under the
22 $GIT_DIR/modules/ directory of its superproject, (ii) a working
23 directory inside the superproject’s working directory, and a .git file
24 at the root of the submodule’s working directory pointing to (i).
25
26 Assuming the submodule has a Git directory at $GIT_DIR/modules/foo/ and
27 a working directory at path/to/bar/, the superproject tracks the
28 submodule via a gitlink entry in the tree at path/to/bar and an entry
29 in its .gitmodules file (see gitmodules(5)) of the form
30 submodule.foo.path = path/to/bar.
31
32 The gitlink entry contains the object name of the commit that the
33 superproject expects the submodule’s working directory to be at.
34
35 The section submodule.foo.* in the .gitmodules file gives additional
36 hints to Git’s porcelain layer. For example, the submodule.foo.url
37 setting specifies where to obtain the submodule.
38
39 Submodules can be used for at least two different use cases:
40
41 1. Using another project while maintaining independent history.
42 Submodules allow you to contain the working tree of another project
43 within your own working tree while keeping the history of both
44 projects separate. Also, since submodules are fixed to an arbitrary
45 version, the other project can be independently developed without
46 affecting the superproject, allowing the superproject project to
47 fix itself to new versions only when desired.
48
49 2. Splitting a (logically single) project into multiple repositories
50 and tying them back together. This can be used to overcome current
51 limitations of Git’s implementation to have finer grained access:
52
53 · Size of the Git repository: In its current form Git scales up
54 poorly for large repositories containing content that is not
55 compressed by delta computation between trees. For example, you
56 can use submodules to hold large binary assets and these
57 repositories can be shallowly cloned such that you do not have
58 a large history locally.
59
60 · Transfer size: In its current form Git requires the whole
61 working tree present. It does not allow partial trees to be
62 transferred in fetch or clone. If the project you work on
63 consists of multiple repositories tied together as submodules
64 in a superproject, you can avoid fetching the working trees of
65 the repositories you are not interested in.
66
67 · Access control: By restricting user access to submodules, this
68 can be used to implement read/write policies for different
69 users.
70
72 Submodule operations can be configured using the following mechanisms
73 (from highest to lowest precedence):
74
75 · The command line for those commands that support taking submodules
76 as part of their pathspecs. Most commands have a boolean flag
77 --recurse-submodules which specify whether to recurse into
78 submodules. Examples are grep and checkout. Some commands take
79 enums, such as fetch and push, where you can specify how submodules
80 are affected.
81
82 · The configuration inside the submodule. This includes
83 $GIT_DIR/config in the submodule, but also settings in the tree
84 such as a .gitattributes or .gitignore files that specify behavior
85 of commands inside the submodule.
86
87 For example an effect from the submodule’s .gitignore file would be
88 observed when you run git status --ignore-submodules=none in the
89 superproject. This collects information from the submodule’s
90 working directory by running status in the submodule while paying
91 attention to the .gitignore file of the submodule.
92
93 The submodule’s $GIT_DIR/config file would come into play when
94 running git push --recurse-submodules=check in the superproject, as
95 this would check if the submodule has any changes not published to
96 any remote. The remotes are configured in the submodule as usual in
97 the $GIT_DIR/config file.
98
99 · The configuration file $GIT_DIR/config in the superproject. Git
100 only recurses into active submodules (see "ACTIVE SUBMODULES"
101 section below).
102
103 If the submodule is not yet initialized, then the configuration
104 inside the submodule does not exist yet, so where to obtain the
105 submodule from is configured here for example.
106
107 · The .gitmodules file inside the superproject. A project usually
108 uses this file to suggest defaults for the upstream collection of
109 repositories for the mapping that is required between a submodule’s
110 name and its path.
111
112 This file mainly serves as the mapping between the name and path of
113 submodules in the superproject, such that the submodule’s Git
114 directory can be located.
115
116 If the submodule has never been initialized, this is the only place
117 where submodule configuration is found. It serves as the last
118 fallback to specify where to obtain the submodule from.
119
121 Submodules can take the following forms:
122
123 · The basic form described in DESCRIPTION with a Git directory, a
124 working directory, a gitlink, and a .gitmodules entry.
125
126 · "Old-form" submodule: A working directory with an embedded .git
127 directory, and the tracking gitlink and .gitmodules entry in the
128 superproject. This is typically found in repositories generated
129 using older versions of Git.
130
131 It is possible to construct these old form repositories manually.
132
133 When deinitialized or deleted (see below), the submodule’s Git
134 directory is automatically moved to $GIT_DIR/modules/<name>/ of the
135 superproject.
136
137 · Deinitialized submodule: A gitlink, and a .gitmodules entry, but no
138 submodule working directory. The submodule’s Git directory may be
139 there as after deinitializing the Git directory is kept around. The
140 directory which is supposed to be the working directory is empty
141 instead.
142
143 A submodule can be deinitialized by running git submodule deinit.
144 Besides emptying the working directory, this command only modifies
145 the superproject’s $GIT_DIR/config file, so the superproject’s
146 history is not affected. This can be undone using git submodule
147 init.
148
149 · Deleted submodule: A submodule can be deleted by running git rm
150 <submodule path> && git commit. This can be undone using git
151 revert.
152
153 The deletion removes the superproject’s tracking data, which are
154 both the gitlink entry and the section in the .gitmodules file. The
155 submodule’s working directory is removed from the file system, but
156 the Git directory is kept around as it to make it possible to
157 checkout past commits without requiring fetching from another
158 repository.
159
160 To completely remove a submodule, manually delete
161 $GIT_DIR/modules/<name>/.
162
164 A submodule is considered active,
165
166 1. if submodule.<name>.active is set to true
167
168 or
169
170 2. if the submodule’s path matches the pathspec in submodule.active
171
172 or
173
174 3. if submodule.<name>.url is set.
175
176 and these are evaluated in this order.
177
178 For example:
179
180 [submodule "foo"]
181 active = false
182 url = https://example.org/foo
183 [submodule "bar"]
184 active = true
185 url = https://example.org/bar
186 [submodule "baz"]
187 url = https://example.org/baz
188
189 In the above config only the submodule bar and baz are active, bar due
190 to (1) and baz due to (3). foo is inactive because (1) takes precedence
191 over (3)
192
193 Note that (3) is a historical artefact and will be ignored if the (1)
194 and (2) specify that the submodule is not active. In other words, if we
195 have a submodule.<name>.active set to false or if the submodule’s path
196 is excluded in the pathspec in submodule.active, the url doesn’t matter
197 whether it is present or not. This is illustrated in the example that
198 follows.
199
200 [submodule "foo"]
201 active = true
202 url = https://example.org/foo
203 [submodule "bar"]
204 url = https://example.org/bar
205 [submodule "baz"]
206 url = https://example.org/baz
207 [submodule "bob"]
208 ignore = true
209 [submodule]
210 active = b*
211 active = :(exclude) baz
212
213 In here all submodules except baz (foo, bar, bob) are active. foo due
214 to its own active flag and all the others due to the submodule active
215 pathspec, which specifies that any submodule starting with b except baz
216 are also active, regardless of the presence of the .url field.
217
219 # add a submodule
220 git submodule add <url> <path>
221
222 # occasionally update the submodule to a new version:
223 git -C <path> checkout <new version>
224 git add <path>
225 git commit -m "update submodule to new version"
226
227 # See the list of submodules in a superproject
228 git submodule status
229
230 # See FORMS on removing submodules
231
233 # Enable recursion for relevant commands, such that
234 # regular commands recurse into submodules by default
235 git config --global submodule.recurse true
236
237 # Unlike the other commands below clone still needs
238 # its own recurse flag:
239 git clone --recurse <URL> <directory>
240 cd <directory>
241
242 # Get to know the code:
243 git grep foo
244 git ls-files
245
246 # Get new code
247 git fetch
248 git pull --rebase
249
250 # change worktree
251 git checkout
252 git reset
253
255 When cloning or pulling a repository containing submodules the
256 submodules will not be checked out by default; You can instruct clone
257 to recurse into submodules. The init and update subcommands of git
258 submodule will maintain submodules checked out and at an appropriate
259 revision in your working tree. Alternatively you can set
260 submodule.recurse to have checkout recursing into submodules.
261
263 git-submodule(1), gitmodules(5).
264
266 Part of the git(1) suite
267
268
269
270Git 2.24.1 12/10/2019 GITSUBMODULES(7)