interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / Documentation / gitsubmodules.adoc

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