(window.webpackJsonp=window.webpackJsonp||[]).push([[1154],{1561:function(e,t,o){"use strict";o.r(t);var a=o(31),s=Object(a.a)({},(function(){var e=this,t=e.$createElement,o=e._self._c||t;return o("ContentSlotsDistributor",{attrs:{"slot-key":e.$parent.slotKey}},[o("h1",{attrs:{id:"submodules"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#submodules"}},[e._v("#")]),e._v(" Submodules")]),e._v(" "),o("h2",{attrs:{id:"cloning-a-git-repository-having-submodules"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#cloning-a-git-repository-having-submodules"}},[e._v("#")]),e._v(" Cloning a Git repository having submodules")]),e._v(" "),o("p",[e._v("When you clone a repository that uses submodules, you'll need to initialize and update them.")]),e._v(" "),o("div",{staticClass:"language-git extra-class"},[o("pre",{pre:!0,attrs:{class:"language-git"}},[o("code",[o("span",{pre:!0,attrs:{class:"token command"}},[e._v("$ git clone"),o("span",{pre:!0,attrs:{class:"token parameter"}},[e._v(" --recursive")]),e._v(" https://github.com/username/repo.git")]),e._v("\n\n")])])]),o("p",[e._v("This will clone the referenced submodules and place them in the appropriate folders (including submodules within submodules). This is equivalent to running "),o("code",[e._v("git submodule update --init --recursive")]),e._v(" immediately after the clone is finished.")]),e._v(" "),o("h2",{attrs:{id:"updating-a-submodule"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#updating-a-submodule"}},[e._v("#")]),e._v(" Updating a Submodule")]),e._v(" "),o("p",[e._v("A submodule references a specific commit in another repository. To check out the exact state that is referenced for all submodules, run")]),e._v(" "),o("div",{staticClass:"language-git extra-class"},[o("pre",{pre:!0,attrs:{class:"language-git"}},[o("code",[e._v("git submodule update --recursive\n\n")])])]),o("p",[e._v("Sometimes instead of using the state that is referenced you want to update to your local checkout to the latest state of that submodule on a remote.\nTo check out all submodules to the latest state on the remote with a single command, you can use")]),e._v(" "),o("div",{staticClass:"language-git extra-class"},[o("pre",{pre:!0,attrs:{class:"language-git"}},[o("code",[e._v("git submodule foreach git pull \n\n")])])]),o("p",[e._v("or use the default "),o("code",[e._v("git pull")]),e._v(" arguments")]),e._v(" "),o("div",{staticClass:"language-git extra-class"},[o("pre",{pre:!0,attrs:{class:"language-git"}},[o("code",[e._v("git submodule foreach git pull\n\n")])])]),o("p",[e._v("Note that this will just update your local working copy. Running "),o("code",[e._v("git status")]),e._v(" will list the submodule directory as dirty if it changed because of this command. To update your repository to reference the new state instead, you have to commit the changes:")]),e._v(" "),o("div",{staticClass:"language-git extra-class"},[o("pre",{pre:!0,attrs:{class:"language-git"}},[o("code",[e._v("git add \ngit commit\n\n")])])]),o("p",[e._v("There might be some changes you have that can have merge conflict if you use "),o("code",[e._v("git pull")]),e._v(" so you can use "),o("code",[e._v("git pull --rebase")]),e._v(" to rewind your changes to top, most of the time it decreases the chances of conflict. Also it pulls all the branches to local.")]),e._v(" "),o("div",{staticClass:"language-git extra-class"},[o("pre",{pre:!0,attrs:{class:"language-git"}},[o("code",[e._v("git submodule foreach git pull --rebase\n\n")])])]),o("p",[e._v("To checkout the latest state of a specific submodule, you can use :")]),e._v(" "),o("div",{staticClass:"language-git extra-class"},[o("pre",{pre:!0,attrs:{class:"language-git"}},[o("code",[e._v("git submodule update --remote \n\n")])])]),o("h2",{attrs:{id:"adding-a-submodule"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#adding-a-submodule"}},[e._v("#")]),e._v(" Adding a submodule")]),e._v(" "),o("p",[e._v("You can include another Git repository as a folder within your project, tracked by Git:")]),e._v(" "),o("div",{staticClass:"language-git extra-class"},[o("pre",{pre:!0,attrs:{class:"language-git"}},[o("code",[o("span",{pre:!0,attrs:{class:"token command"}},[e._v("$ git submodule add https://github.com/jquery/jquery.git")]),e._v("\n\n")])])]),o("p",[e._v("You should add and commit the new "),o("code",[e._v(".gitmodules")]),e._v(" file; this tells Git what submodules should be cloned when "),o("code",[e._v("git submodule update")]),e._v(" is run.")]),e._v(" "),o("h2",{attrs:{id:"setting-a-submodule-to-follow-a-branch"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#setting-a-submodule-to-follow-a-branch"}},[e._v("#")]),e._v(" Setting a submodule to follow a branch")]),e._v(" "),o("p",[e._v('A submodule is always checked out at a specific commit SHA1 (the "gitlink", special entry in the index of the parent repo)')]),e._v(" "),o("p",[e._v("But one can request to update that submodule to the latest commit of a branch of the submodule remote repo.")]),e._v(" "),o("p",[e._v("Rather than going in each submodule, doing a "),o("code",[e._v("git checkout abranch --track origin/abranch, git pull")]),e._v(", you can simply do (from the parent repo) a:")]),e._v(" "),o("div",{staticClass:"language-git extra-class"},[o("pre",{pre:!0,attrs:{class:"language-git"}},[o("code",[e._v("git submodule update --remote --recursive\n\n")])])]),o("p",[e._v("Since the SHA1 of the submodule would change, you would still need to follow that with:")]),e._v(" "),o("div",{staticClass:"language-git extra-class"},[o("pre",{pre:!0,attrs:{class:"language-git"}},[o("code",[e._v("git add .\ngit commit -m "),o("span",{pre:!0,attrs:{class:"token string"}},[e._v('"update submodules"')]),e._v("\n\n")])])]),o("p",[e._v("That supposes the submodules were:")]),e._v(" "),o("li",[e._v("\neither added with a branch to follow:\n"),o("div",{staticClass:"language-git extra-class"},[o("pre",{pre:!0,attrs:{class:"language-git"}},[o("code",[e._v(" git submodule -b abranch -- /url/of/submodule/repo\n\n")])])])]),e._v(" "),o("li",[e._v("\nor configured (for an existing submodule) to follow a branch:\n"),o("div",{staticClass:"language-git extra-class"},[o("pre",{pre:!0,attrs:{class:"language-git"}},[o("code",[e._v(" cd /path/to/parent/repo\n git config -f .gitmodules submodule.asubmodule.branch abranch\n\n")])])])]),e._v(" "),o("h2",{attrs:{id:"moving-a-submodule"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#moving-a-submodule"}},[e._v("#")]),e._v(" Moving a submodule")]),e._v(" "),o("p",[e._v("Run:")]),e._v(" "),o("div",{staticClass:"language-git extra-class"},[o("pre",{pre:!0,attrs:{class:"language-git"}},[o("code",[o("span",{pre:!0,attrs:{class:"token command"}},[e._v("$ git mv **old/path/to/module** **new/path/to/module**")]),e._v("\n\n1.8\n
    \n
  1. \nEdit `.gitmodules` and change the path of the submodule appropriately, and put it in the index with `git add .gitmodules`.\n
  2. \n
  3. \nIf needed, create the parent directory of the new location of the submodule (`mkdir -p **new/path/to**`).\n
  4. \n
  5. \nMove all content from the old to the new directory (`mv -vi **old/path/to/module** **new/path/to/submodule**`).\n
  6. \n
  7. \nMake sure Git tracks this directory (`git add **new/path**/to`).\n
  8. \n
  9. \nRemove the old directory with `git rm --cached **old/path/to/module**`.\n
  10. \n
  11. \nMove the directory `.git/modules/**old/path/to/module**` with all its content to `.git/modules/**new/path/to/module**`.\n
  12. \n
  13. \n

    Edit the `.git/modules/**new/path/to**/config` file, make sure that worktree item points to the new locations, so in this example it should be `worktree = ../../../../../**old/path/to/module**`. Typically there should be two more `..` then directories in the direct path in that place.\n. Edit the file `**new/path/to/module**/.git`, make sure that the path in it points to the correct new location inside the main project `.git` folder, so in this example `gitdir: ../../../.git/modules/**new/path/to/module**`.

    \n`git status` output looks like this afterwards:\n\n```git\n # On branch master\n # Changes to be committed:\n # (use "),o("span",{pre:!0,attrs:{class:"token string"}},[e._v('"git reset HEAD ..."')]),e._v(" to unstage)\n #\n # modified: .gitmodules\n # renamed: old/path/to/submodule -> new/path/to/submodule\n #\n\n")])])]),o("li",[e._v("\nEdit `.gitmodules` and change the path of the submodule appropriately, and put it in the index with `git add .gitmodules`.\n")]),e._v(" "),o("li",[e._v("\nIf needed, create the parent directory of the new location of the submodule (`mkdir -p **new/path/to**`).\n")]),e._v(" "),o("li",[e._v("\nMove all content from the old to the new directory (`mv -vi **old/path/to/module** **new/path/to/submodule**`).\n")]),e._v(" "),o("li",[e._v("\nMake sure Git tracks this directory (`git add **new/path**/to`).\n")]),e._v(" "),o("li",[e._v("\nRemove the old directory with `git rm --cached **old/path/to/module**`.\n")]),e._v(" "),o("li",[e._v("\nMove the directory `.git/modules/**old/path/to/module**` with all its content to `.git/modules/**new/path/to/module**`.\n")]),e._v(" "),o("li",[o("p",[e._v("Edit the `.git/modules/**new/path/to**/config` file, make sure that worktree item points to the new locations, so in this example it should be `worktree = ../../../../../**old/path/to/module**`. Typically there should be two more `..` then directories in the direct path in that place.\n. Edit the file `**new/path/to/module**/.git`, make sure that the path in it points to the correct new location inside the main project `.git` folder, so in this example `gitdir: ../../../.git/modules/**new/path/to/module**`.")]),e._v("\n`git status` output looks like this afterwards:\n"),o("div",{staticClass:"language-git extra-class"},[o("pre",{pre:!0,attrs:{class:"language-git"}},[o("code",[e._v(" # On branch master\n # Changes to be committed:\n # (use "),o("span",{pre:!0,attrs:{class:"token string"}},[e._v('"git reset HEAD ..."')]),e._v(" to unstage)\n #\n # modified: .gitmodules\n # renamed: old/path/to/submodule -> new/path/to/submodule\n #\n\n")])])])]),e._v(" "),o("li",[e._v("\nFinally, commit the changes.\n")]),e._v(" "),o("p",[e._v("This example from "),o("a",{attrs:{href:"http://stackoverflow.com/a/6310246",target:"_blank",rel:"noopener noreferrer"}},[e._v("Stack Overflow"),o("OutboundLink")],1),e._v(", by "),o("a",{attrs:{href:"http://stackoverflow.com/users/793172",target:"_blank",rel:"noopener noreferrer"}},[e._v("Axel Beckert"),o("OutboundLink")],1)]),e._v(" "),o("h2",{attrs:{id:"removing-a-submodule"}},[o("a",{staticClass:"header-anchor",attrs:{href:"#removing-a-submodule"}},[e._v("#")]),e._v(" Removing a submodule")]),e._v(" "),o("p",[e._v("You can remove a submodule (e.g. "),o("code",[e._v("the_submodule")]),e._v(") by calling:")]),e._v(" "),o("div",{staticClass:"language-git extra-class"},[o("pre",{pre:!0,attrs:{class:"language-git"}},[o("code",[o("span",{pre:!0,attrs:{class:"token command"}},[e._v("$ git submodule deinit the_submodule")]),e._v("\n"),o("span",{pre:!0,attrs:{class:"token command"}},[e._v("$ git rm the_submodule ")]),e._v("\n\n")])])]),o("li",[e._v("\n`git submodule deinit the_submodule` deletes `the_submodule`s' entry from .git/config. This excludes the_submodule from `git submodule update`, `git submodule sync` and `git submodule foreach` calls and deletes its local content [(source)](https://git-scm.com/docs/git-submodule#git-submodule-deinit). Also, this will not be shown as change in your parent repository. `git submodule init` and `git submodule update` will restore the submodule, again without commitable changes in your parent repository.\n")]),e._v(" "),o("li",[e._v("\n`git rm the_submodule` will remove the submodule from the work tree. The files will be gone as well as the submodules' entry in the `.gitmodules` file [(source)](https://git-scm.com/docs/git-rm#_submodules). If only `git rm the_submodule` (without prior `git submodule deinit the_submodule` is run, however, the submodules' entry in your .git/config file will remain.\n")]),e._v(" "),o("p",[e._v("Taken from "),o("a",{attrs:{href:"http://stackoverflow.com/a/1260982/7598462",target:"_blank",rel:"noopener noreferrer"}},[e._v("here"),o("OutboundLink")],1),e._v(":")]),e._v(" "),o("ol",[o("li",[e._v("Delete the relevant section from the "),o("code",[e._v(".gitmodules")]),e._v(" file.")]),e._v(" "),o("li",[e._v("Stage the "),o("code",[e._v(".gitmodules")]),e._v(" changes "),o("code",[e._v("git add .gitmodules")])]),e._v(" "),o("li",[e._v("Delete the relevant section from "),o("code",[e._v(".git/config")]),e._v(".")]),e._v(" "),o("li",[e._v("Run "),o("code",[e._v("git rm --cached path_to_submodule")]),e._v(" (no trailing slash).")]),e._v(" "),o("li",[e._v("Run "),o("code",[e._v("rm -rf .git/modules/path_to_submodule")])]),e._v(" "),o("li",[e._v("Commit "),o("code",[e._v('git commit -m "Removed submodule "')])]),e._v(" "),o("li",[e._v("Delete the now untracked submodule files")]),e._v(" "),o("li",[o("code",[e._v("rm -rf path_to_submodule")])])])])}),[],!1,null,null,null);t.default=s.exports}}]);