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