1gitsubmodules(7) 2================ 3 4NAME 5---- 6gitsubmodules - mounting one repository inside another 7 8SYNOPSIS 9-------- 10 .gitmodules, $GIT_DIR/config 11------------------ 12git submodule 13git <command> --recurse-submodules 14------------------ 15 16DESCRIPTION 17----------- 18 19A submodule is a repository embedded inside another repository. 20The submodule has its own history; the repository it is embedded 21in is called a superproject. 22 23On the filesystem, a submodule usually (but not always - see FORMS below) 24consists of (i) a Git directory located under the `$GIT_DIR/modules/` 25directory of its superproject, (ii) a working directory inside the 26superproject's working directory, and a `.git` file at the root of 27the submodule’s working directory pointing to (i). 28 29Assuming the submodule has a Git directory at `$GIT_DIR/modules/foo/` 30and a working directory at `path/to/bar/`, the superproject tracks the 31submodule via a `gitlink` entry in the tree at `path/to/bar` and an entry 32in its `.gitmodules` file (see linkgit:gitmodules[5]) of the form 33`submodule.foo.path = path/to/bar`. 34 35The `gitlink` entry contains the object name of the commit that the 36superproject expects the submodule’s working directory to be at. 37 38The section `submodule.foo.*` in the `.gitmodules` file gives additional 39hints to Gits porcelain layer such as where to obtain the submodule via 40the `submodule.foo.url` setting. 41 42Submodules can be used for at least two different use cases: 43 441. 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 522. 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 70The configuration of submodules 71------------------------------- 72 73Submodule 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+ 87For example an effect from the submodule's `.gitignore` file 88would be observed when you run `git status --ignore-submodules=none` in 89the superproject. This collects information from the submodule's working 90directory by running `status` in the submodule, which does pay attention 91to its `.gitignore` file. 92+ 93The submodule's `$GIT_DIR/config` file would come into play when running 94`git push --recurse-submodules=check` in the superproject, as this would 95check if the submodule has any changes not published to any remote. The 96remotes are configured in the submodule as usual in the `$GIT_DIR/config` 97file. 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+ 103If the submodule is not yet initialized, then the configuration 104inside the submodule does not exist yet, so configuration where to 105obtain 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+ 112This file mainly serves as the mapping between name and path in 113the superproject, such that the submodule's git directory can be 114located. 115+ 116If the submodule has never been initialized, this is the only place 117where submodule configuration is found. It serves as the last fallback 118to specify where to obtain the submodule from. 119 120FORMS 121----- 122 123Submodules can take the following forms: 124 125 * The basic form described in DESCRIPTION with a Git directory, 126a 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 130the superproject. This is typically found in repositories generated 131using older versions of Git. 132+ 133It is possible to construct these old form repositories manually. 134+ 135When deinitialized or deleted (see below), the submodule’s Git 136directory is automatically moved to `$GIT_DIR/modules/<name>/` 137of the superproject. 138 139 * Deinitialized submodule: A `gitlink`, and a `.gitmodules` entry, 140but no submodule working directory. The submodule’s git directory 141may be there as after deinitializing the git directory is kept around. 142The directory which is supposed to be the working directory is empty instead. 143+ 144A submodule can be deinitialized by running `git submodule deinit`. 145Besides emptying the working directory, this command only modifies 146the superproject’s `$GIT_DIR/config` file, so the superproject’s history 147is 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 151using `git revert`. 152+ 153The deletion removes the superproject’s tracking data, which are 154both the `gitlink` entry and the section in the `.gitmodules` file. 155The submodule’s working directory is removed from the file 156system, but the Git directory is kept around as it to make it 157possible to checkout past commits without requiring fetching 158from another repository. 159+ 160To completely remove a submodule, manually delete 161`$GIT_DIR/modules/<name>/`. 162 163Workflow 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 180Workflow 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 204Implementation details 205---------------------- 206 207When cloning or pulling a repository containing submodules the submodules 208will not be checked out by default; You can instruct 'clone' to recurse 209into submodules. The 'init' and 'update' subcommands of 'git submodule' 210will maintain submodules checked out and at an appropriate revision in 211your working tree. Alternatively you can set 'submodule.recurse' to have 212'checkout' recursing into submodules. 213 214 215SEE ALSO 216-------- 217linkgit:git-submodule[1], linkgit:gitmodules[5]. 218 219GIT 220--- 221Part of the linkgit:git[1] suite