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 (a) if `submodule.<name>.active` is set to `true`
167 or
168 (b) if the submodule's path matches the pathspec in `submodule.active`
169 or
170 (c) if `submodule.<name>.url` is set.
171
172 and these are evaluated in this order.
173
174 For example:
175
176 [submodule "foo"]
177 active = false
178 url = https://example.org/foo
179 [submodule "bar"]
180 active = true
181 url = https://example.org/bar
182 [submodule "baz"]
183 url = https://example.org/baz
184
185 In the above config only the submodule bar and baz are active, bar due
186 to (a) and baz due to (c). foo is inactive because (a) takes precedence
187 over (c)
188
189 Note that (c) is a historical artefact and will be ignored if the (a)
190 and (b) specify that the submodule is not active. In other words, if we
191 have an submodule.<name>.active set to false or if the submodule’s path
192 is excluded in the pathspec in submodule.active, the url doesn’t matter
193 whether it is present or not. This is illustrated in the example that
194 follows.
195
196 [submodule "foo"]
197 active = true
198 url = https://example.org/foo
199 [submodule "bar"]
200 url = https://example.org/bar
201 [submodule "baz"]
202 url = https://example.org/baz
203 [submodule "bob"]
204 ignore = true
205 [submodule]
206 active = b*
207 active = :(exclude) baz
208
209 In here all submodules except baz (foo, bar, bob) are active. foo due
210 to its own active flag and all the others due to the submodule active
211 pathspec, which specifies that any submodule starting with b except baz
212 are also active, regardless of the presence of the .url field.
213
215 # add a submodule
216 git submodule add <url> <path>
217
218 # occasionally update the submodule to a new version:
219 git -C <path> checkout <new version>
220 git add <path>
221 git commit -m "update submodule to new version"
222
223 # See the list of submodules in a superproject
224 git submodule status
225
226 # See FORMS on removing submodules
227
229 # Enable recursion for relevant commands, such that
230 # regular commands recurse into submodules by default
231 git config --global submodule.recurse true
232
233 # Unlike the other commands below clone still needs
234 # its own recurse flag:
235 git clone --recurse <URL> <directory>
236 cd <directory>
237
238 # Get to know the code:
239 git grep foo
240 git ls-files
241
242 # Get new code
243 git fetch
244 git pull --rebase
245
246 # change worktree
247 git checkout
248 git reset
249
251 When cloning or pulling a repository containing submodules the
252 submodules will not be checked out by default; You can instruct clone
253 to recurse into submodules. The init and update subcommands of git
254 submodule will maintain submodules checked out and at an appropriate
255 revision in your working tree. Alternatively you can set
256 submodule.recurse to have checkout recursing into submodules.
257
259 git-submodule(1), gitmodules(5).
260
262 Part of the git(1) suite
263
264
265
266Git 2.18.1 05/14/2019 GITSUBMODULES(7)