git
[Ported from github.com/git/git] Sync with Git 2.56.1
Brought by drakestapleton
git / Documentation / gitsubmodules.adoc
| 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 | ------------------ |
| 13 | git submodule |
| 14 | git <command> --recurse-submodules |
| 15 | ------------------ |
| 16 | |
| 17 | DESCRIPTION |
| 18 | ----------- |
| 19 | |
| 20 | A submodule is a repository embedded inside another repository. |
| 21 | The submodule has its own history; the repository it is embedded |
| 22 | in is called a superproject. |
| 23 | |
| 24 | On the filesystem, a submodule usually |
| 25 | (but not always - see <<FORMS,FORMS>> below) |
| 26 | consists of (i) a Git directory located under the `$GIT_DIR/modules/` |
| 27 | directory of its superproject, (ii) a working directory inside the |
| 28 | superproject's working directory, and a `.git` file at the root of |
| 29 | the submodule's working directory pointing to (i). |
| 30 | |
| 31 | Assuming the submodule has a Git directory at `$GIT_DIR/modules/foo/` |
| 32 | and a working directory at `path/to/bar/`, the superproject tracks the |
| 33 | submodule via a `gitlink` entry in the tree at `path/to/bar` and an entry |
| 34 | in its `.gitmodules` file (see linkgit:gitmodules[5]) of the form |
| 35 | `submodule.foo.path = path/to/bar`. |
| 36 | |
| 37 | The `gitlink` entry contains the object name of the commit that the |
| 38 | superproject expects the submodule's working directory to be at. |
| 39 | |
| 40 | The section `submodule.foo.*` in the `.gitmodules` file gives additional |
| 41 | hints to Git's porcelain layer. For example, the `submodule.foo.url` |
| 42 | setting specifies where to obtain the submodule. |
| 43 | |
| 44 | Submodules can be used for at least two different use cases: |
| 45 | |
| 46 | 1. 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 | |
| 54 | 2. 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 | |
| 75 | The configuration of submodules |
| 76 | ------------------------------- |
| 77 | |
| 78 | Submodule 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 | + |
| 93 | For example an effect from the submodule's `.gitignore` file |
| 94 | would be observed when you run `git status --ignore-submodules=none` in |
| 95 | the superproject. This collects information from the submodule's working |
| 96 | directory by running `status` in the submodule while paying attention |
| 97 | to the `.gitignore` file of the submodule. |
| 98 | + |
| 99 | The submodule's `$GIT_DIR/config` file would come into play when running |
| 100 | `git push --recurse-submodules=check` in the superproject, as this would |
| 101 | check if the submodule has any changes not published to any remote. The |
| 102 | remotes are configured in the submodule as usual in the `$GIT_DIR/config` |
| 103 | file. |
| 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 | + |
| 109 | If the submodule is not yet initialized, then the configuration |
| 110 | inside the submodule does not exist yet, so where to |
| 111 | obtain 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 | + |
| 118 | This file mainly serves as the mapping between the name and path of submodules |
| 119 | in the superproject, such that the submodule's Git directory can be |
| 120 | located. |
| 121 | + |
| 122 | If the submodule has never been initialized, this is the only place |
| 123 | where submodule configuration is found. It serves as the last fallback |
| 124 | to specify where to obtain the submodule from. |
| 125 | |
| 126 | [[FORMS]] |
| 127 | FORMS |
| 128 | ----- |
| 129 | |
| 130 | Submodules can take the following forms: |
| 131 | |
| 132 | * The basic form described in DESCRIPTION with a Git directory, |
| 133 | a 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 |
| 137 | the superproject. This is typically found in repositories generated |
| 138 | using older versions of Git. |
| 139 | + |
| 140 | It is possible to construct these old form repositories manually. |
| 141 | + |
| 142 | When deinitialized or deleted (see below), the submodule's Git |
| 143 | directory is automatically moved to `$GIT_DIR/modules/<name>/` |
| 144 | of the superproject. |
| 145 | |
| 146 | * Deinitialized submodule: A `gitlink`, and a `.gitmodules` entry, |
| 147 | but no submodule working directory. The submodule's Git directory |
| 148 | may be there as after deinitializing the Git directory is kept around. |
| 149 | The directory which is supposed to be the working directory is empty instead. |
| 150 | + |
| 151 | A submodule can be deinitialized by running `git submodule deinit`. |
| 152 | Besides emptying the working directory, this command only modifies |
| 153 | the superproject's `$GIT_DIR/config` file, so the superproject's history |
| 154 | is 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 |
| 158 | using `git revert`. |
| 159 | + |
| 160 | The deletion removes the superproject's tracking data, which are |
| 161 | both the `gitlink` entry and the section in the `.gitmodules` file. |
| 162 | The submodule's working directory is removed from the file |
| 163 | system, but the Git directory is kept around as it to make it |
| 164 | possible to checkout past commits without requiring fetching |
| 165 | from another repository. |
| 166 | + |
| 167 | To completely remove a submodule, manually delete |
| 168 | `$GIT_DIR/modules/<name>/`. |
| 169 | |
| 170 | [[ACTIVE_SUBMODULES]] |
| 171 | ACTIVE SUBMODULES |
| 172 | ----------------- |
| 173 | |
| 174 | A submodule is considered active, |
| 175 | |
| 176 | 1. if `submodule.<name>.active` is set to `true` |
| 177 | + |
| 178 | or |
| 179 | |
| 180 | 2. if the submodule's path matches the pathspec in `submodule.active` |
| 181 | + |
| 182 | or |
| 183 | |
| 184 | 3. if `submodule.<name>.url` is set. |
| 185 | |
| 186 | and these are evaluated in this order. |
| 187 | |
| 188 | For 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 | |
| 199 | In 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 | |
| 203 | Note 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, |
| 205 | if we have a `submodule.<name>.active` set to `false` or if the |
| 206 | submodule's path is excluded in the pathspec in `submodule.active`, the |
| 207 | url doesn't matter whether it is present or not. This is illustrated in |
| 208 | the 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 | |
| 223 | In 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 |
| 225 | submodule active pathspec, which specifies that any submodule |
| 226 | starting with 'b' except 'baz' are also active, regardless of the |
| 227 | presence of the .url field. |
| 228 | |
| 229 | Workflow 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 | |
| 246 | Workflow 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 | |
| 273 | Implementation details |
| 274 | ---------------------- |
| 275 | |
| 276 | When cloning or pulling a repository containing submodules the submodules |
| 277 | will not be checked out by default; you can instruct `clone` to recurse |
| 278 | into submodules. The `init` and `update` subcommands of `git submodule` |
| 279 | will maintain submodules checked out and at an appropriate revision in |
| 280 | your working tree. Alternatively you can set `submodule.recurse` to have |
| 281 | `checkout` recurse into submodules (note that `submodule.recurse` also |
| 282 | affects other Git commands, see linkgit:git-config[1] for a complete list). |
| 283 | |
| 284 | |
| 285 | SEE ALSO |
| 286 | -------- |
| 287 | linkgit:git-submodule[1], linkgit:gitmodules[5]. |
| 288 | |
| 289 | GIT |
| 290 | --- |
| 291 | Part of the linkgit:git[1] suite |