Skip to content

Commit d480345

Browse files
stefanbellergitster
authored andcommitted
submodules: overhaul documentation
This patch aims to detangle (a) the usage of `git-submodule` from (b) the concept of submodules and (c) how the actual implementation looks like, such as where they are configured and (d) what the best practices are. To do so, move the conceptual parts of the 'git-submodule' man page to a new man page gitsubmodules(7). This new page is just like gitmodules(5), gitattributes(5), gitcredentials(7), gitnamespaces(7), gittutorial(7), which introduce a concept rather than explaining a specific command. Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 41dd433 commit d480345

File tree

4 files changed

+234
-36
lines changed

4 files changed

+234
-36
lines changed

Documentation/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ MAN7_TXT += giteveryday.txt
3131
MAN7_TXT += gitglossary.txt
3232
MAN7_TXT += gitnamespaces.txt
3333
MAN7_TXT += gitrevisions.txt
34+
MAN7_TXT += gitsubmodules.txt
3435
MAN7_TXT += gittutorial-2.txt
3536
MAN7_TXT += gittutorial.txt
3637
MAN7_TXT += gitworkflows.txt

Documentation/git-rm.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ Ignored files are deemed expendable and won't stop a submodule's work
152152
tree from being removed.
153153

154154
If you only want to remove the local checkout of a submodule from your
155-
work tree without committing the removal,
156-
use linkgit:git-submodule[1] `deinit` instead.
155+
work tree without committing the removal, use linkgit:git-submodule[1] `deinit`
156+
instead. Also see linkgit:gitsubmodules[7] for details on submodule removal.
157157

158158
EXAMPLES
159159
--------

Documentation/git-submodule.txt

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,37 +24,7 @@ DESCRIPTION
2424
-----------
2525
Inspects, updates and manages submodules.
2626

27-
A submodule allows you to keep another Git repository in a subdirectory
28-
of your repository. The other repository has its own history, which does not
29-
interfere with the history of the current repository. This can be used to
30-
have external dependencies such as third party libraries for example.
31-
32-
When cloning or pulling a repository containing submodules however,
33-
these will not be checked out by default; the 'init' and 'update'
34-
subcommands will maintain submodules checked out and at
35-
appropriate revision in your working tree.
36-
37-
Submodules are composed from a so-called `gitlink` tree entry
38-
in the main repository that refers to a particular commit object
39-
within the inner repository that is completely separate.
40-
A record in the `.gitmodules` (see linkgit:gitmodules[5]) file at the
41-
root of the source tree assigns a logical name to the submodule and
42-
describes the default URL the submodule shall be cloned from.
43-
The logical name can be used for overriding this URL within your
44-
local repository configuration (see 'submodule init').
45-
46-
Submodules are not to be confused with remotes, which are other
47-
repositories of the same project; submodules are meant for
48-
different projects you would like to make part of your source tree,
49-
while the history of the two projects still stays completely
50-
independent and you cannot modify the contents of the submodule
51-
from within the main project.
52-
If you want to merge the project histories and want to treat the
53-
aggregated whole as a single project from then on, you may want to
54-
add a remote for the other project and use the 'subtree' merge strategy,
55-
instead of treating the other project as a submodule. Directories
56-
that come from both projects can be cloned and checked out as a whole
57-
if you choose to go that route.
27+
For more information about submodules, see linkgit:gitsubmodules[7].
5828

5929
COMMANDS
6030
--------
@@ -149,15 +119,17 @@ deinit [-f|--force] (--all|[--] <path>...)::
149119
tree. Further calls to `git submodule update`, `git submodule foreach`
150120
and `git submodule sync` will skip any unregistered submodules until
151121
they are initialized again, so use this command if you don't want to
152-
have a local checkout of the submodule in your working tree anymore. If
153-
you really want to remove a submodule from the repository and commit
154-
that use linkgit:git-rm[1] instead.
122+
have a local checkout of the submodule in your working tree anymore.
155123
+
156124
When the command is run without pathspec, it errors out,
157125
instead of deinit-ing everything, to prevent mistakes.
158126
+
159127
If `--force` is specified, the submodule's working tree will
160128
be removed even if it contains local modifications.
129+
+
130+
If you really want to remove a submodule from the repository and commit
131+
that use linkgit:git-rm[1] instead. See linkgit:gitsubmodules[7] for removal
132+
options.
161133

162134
update [--init] [--remote] [-N|--no-fetch] [--[no-]recommend-shallow] [-f|--force] [--checkout|--rebase|--merge] [--reference <repository>] [--depth <depth>] [--recursive] [--jobs <n>] [--] [<path>...]::
163135
+
@@ -435,6 +407,10 @@ This file should be formatted in the same way as `$GIT_DIR/config`. The key
435407
to each submodule url is "submodule.$name.url". See linkgit:gitmodules[5]
436408
for details.
437409

410+
SEE ALSO
411+
--------
412+
linkgit:gitsubmodules[7], linkgit:gitmodules[5].
413+
438414
GIT
439415
---
440416
Part of the linkgit:git[1] suite

Documentation/gitsubmodules.txt

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
gitsubmodules(7)
2+
================
3+
4+
NAME
5+
----
6+
gitsubmodules - mounting one repository inside another
7+
8+
SYNOPSIS
9+
--------
10+
.gitmodules, $GIT_DIR/config
11+
------------------
12+
git submodule
13+
git <command> --recurse-submodules
14+
------------------
15+
16+
DESCRIPTION
17+
-----------
18+
19+
A submodule is a repository embedded inside another repository.
20+
The submodule has its own history; the repository it is embedded
21+
in is called a superproject.
22+
23+
On the filesystem, a submodule usually (but not always - see FORMS below)
24+
consists of (i) a Git directory located under the `$GIT_DIR/modules/`
25+
directory of its superproject, (ii) a working directory inside the
26+
superproject's working directory, and a `.git` file at the root of
27+
the submodule’s working directory pointing to (i).
28+
29+
Assuming the submodule has a Git directory at `$GIT_DIR/modules/foo/`
30+
and a working directory at `path/to/bar/`, the superproject tracks the
31+
submodule via a `gitlink` entry in the tree at `path/to/bar` and an entry
32+
in its `.gitmodules` file (see linkgit:gitmodules[5]) of the form
33+
`submodule.foo.path = path/to/bar`.
34+
35+
The `gitlink` entry contains the object name of the commit that the
36+
superproject expects the submodule’s working directory to be at.
37+
38+
The section `submodule.foo.*` in the `.gitmodules` file gives additional
39+
hints to Gits porcelain layer such as where to obtain the submodule via
40+
the `submodule.foo.url` setting.
41+
42+
Submodules can be used for at least two different use cases:
43+
44+
1. Using another project while maintaining independent history.
45+
Submodules allow you to contain the working tree of another project
46+
within your own working tree while keeping the history of both
47+
projects separate. Also, since submodules are fixed to an arbitrary
48+
version, the other project can be independently developed without
49+
affecting the superproject, allowing the superproject project to
50+
fix itself to new versions only when desired.
51+
52+
2. Splitting a (logically single) project into multiple
53+
repositories and tying them back together. This can be used to
54+
overcome current limitations of Gits implementation to have
55+
finer grained access:
56+
57+
* Size of the git repository:
58+
In its current form Git scales up poorly for large repositories containing
59+
content that is not compressed by delta computation between trees.
60+
However you can also use submodules to e.g. hold large binary assets
61+
and these repositories are then shallowly cloned such that you do not
62+
have a large history locally.
63+
* Transfer size:
64+
In its current form Git requires the whole working tree present. It
65+
does not allow partial trees to be transferred in fetch or clone.
66+
* Access control:
67+
By restricting user access to submodules, this can be used to implement
68+
read/write policies for different users.
69+
70+
The configuration of submodules
71+
-------------------------------
72+
73+
Submodule operations can be configured using the following mechanisms
74+
(from highest to lowest precedence):
75+
76+
* The command line for those commands that support taking submodule specs.
77+
Most commands have a boolean flag '--recurse-submodules' whether to
78+
recurse into submodules. Examples are `ls-files` or `checkout`.
79+
Some commands take enums, such as `fetch` and `push`, where you can
80+
specify how submodules are affected.
81+
82+
* The configuration inside the submodule. This includes `$GIT_DIR/config`
83+
in the submodule, but also settings in the tree such as a `.gitattributes`
84+
or `.gitignore` files that specify behavior of commands inside the
85+
submodule.
86+
+
87+
For example an effect from the submodule's `.gitignore` file
88+
would be observed when you run `git status --ignore-submodules=none` in
89+
the superproject. This collects information from the submodule's working
90+
directory by running `status` in the submodule, which does pay attention
91+
to its `.gitignore` file.
92+
+
93+
The submodule's `$GIT_DIR/config` file would come into play when running
94+
`git push --recurse-submodules=check` in the superproject, as this would
95+
check if the submodule has any changes not published to any remote. The
96+
remotes are configured in the submodule as usual in the `$GIT_DIR/config`
97+
file.
98+
99+
* The configuration file `$GIT_DIR/config` in the superproject.
100+
Typical configuration at this place is controlling if a submodule
101+
is recursed into at all via the `active` flag for example.
102+
+
103+
If the submodule is not yet initialized, then the configuration
104+
inside the submodule does not exist yet, so configuration where to
105+
obtain the submodule from is configured here for example.
106+
107+
* the `.gitmodules` file inside the superproject. Additionally to the
108+
required mapping between submodule's name and path, a project usually
109+
uses this file to suggest defaults for the upstream collection
110+
of repositories.
111+
+
112+
This file mainly serves as the mapping between name and path in
113+
the superproject, such that the submodule's git directory can be
114+
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 fallback
118+
to specify where to obtain the submodule from.
119+
120+
FORMS
121+
-----
122+
123+
Submodules can take the following forms:
124+
125+
* The basic form described in DESCRIPTION with a Git directory,
126+
a working directory, a `gitlink`, and a `.gitmodules` entry.
127+
128+
* "Old-form" submodule: A working directory with an embedded
129+
`.git` directory, and the tracking `gitlink` and `.gitmodules` entry in
130+
the superproject. This is typically found in repositories generated
131+
using older versions of Git.
132+
+
133+
It is possible to construct these old form repositories manually.
134+
+
135+
When deinitialized or deleted (see below), the submodule’s Git
136+
directory is automatically moved to `$GIT_DIR/modules/<name>/`
137+
of the superproject.
138+
139+
* Deinitialized submodule: A `gitlink`, and a `.gitmodules` entry,
140+
but no submodule working directory. The submodule’s git directory
141+
may be there as after deinitializing the git directory is kept around.
142+
The directory which is supposed to be the working directory is empty instead.
143+
+
144+
A submodule can be deinitialized by running `git submodule deinit`.
145+
Besides emptying the working directory, this command only modifies
146+
the superproject’s `$GIT_DIR/config` file, so the superproject’s history
147+
is not affected. This can be undone using `git submodule init`.
148+
149+
* Deleted submodule: A submodule can be deleted by running
150+
`git rm <submodule path> && git commit`. This can be undone
151+
using `git 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.
155+
The submodule’s working directory is removed from the file
156+
system, but the Git directory is kept around as it to make it
157+
possible to checkout past commits without requiring fetching
158+
from another repository.
159+
+
160+
To completely remove a submodule, manually delete
161+
`$GIT_DIR/modules/<name>/`.
162+
163+
Workflow for a third party library
164+
----------------------------------
165+
166+
# add a submodule
167+
git submodule add <url> <path>
168+
169+
# occasionally update the submodule to a new version:
170+
git -C <path> checkout <new version>
171+
git add <path>
172+
git commit -m "update submodule to new version"
173+
174+
# See the list of submodules in a superproject
175+
git submodule status
176+
177+
# See FORMS on removing submodules
178+
179+
180+
Workflow for an artificially split repo
181+
--------------------------------------
182+
183+
# Enable recursion for relevant commands, such that
184+
# regular commands recurse into submodules by default
185+
git config --global submodule.recurse true
186+
187+
# Unlike the other commands below clone still needs
188+
# its own recurse flag:
189+
git clone --recurse <URL> <directory>
190+
cd <directory>
191+
192+
# Get to know the code:
193+
git grep foo
194+
git ls-files
195+
196+
# Get new code
197+
git fetch
198+
git pull --rebase
199+
200+
# change worktree
201+
git checkout
202+
git reset
203+
204+
Implementation details
205+
----------------------
206+
207+
When cloning or pulling a repository containing submodules the submodules
208+
will not be checked out by default; You can instruct 'clone' to recurse
209+
into submodules. The 'init' and 'update' subcommands of 'git submodule'
210+
will maintain submodules checked out and at an appropriate revision in
211+
your working tree. Alternatively you can set 'submodule.recurse' to have
212+
'checkout' recursing into submodules.
213+
214+
215+
SEE ALSO
216+
--------
217+
linkgit:git-submodule[1], linkgit:gitmodules[5].
218+
219+
GIT
220+
---
221+
Part of the linkgit:git[1] suite

0 commit comments

Comments
 (0)