/workbook/htmls/24-Submodules.html

http://github.com/matthewmccullough/git-workshop · HTML · 53 lines · 31 code · 22 blank · 0 comment · 0 complexity · 29434eb2f968b9b1216f26dc4522c2a0 MD5 · raw file

  1. <link rel="stylesheet" type="text/css" media="screen" href="css/screen.css" />
  2. <h1>Submodules</h1>
  3. <h2>Creating Submodules</h2>
  4. <p>This is the equivalent to SVN externals. <a href="http://www.kernel.org/pub/software/scm/git/docs/git-submodule.html">Submodules are subdirectories of a Git project that point to another Git project</a>.</p>
  5. <p>Starting in an existing Git repository, add a submodule.</p>
  6. <pre><code>git submodule add git://somehost/someproject.git mysubproj
  7. </code></pre>
  8. <p>This will create a subfolder named <code>mysubproj</code> in the current directory and a new file called <code>.gitmodules</code> that contains mappings to the repositories.</p>
  9. <p>Git tracks the submodule and the parent module separately. To change, update, or modify the submodule, <code>cd</code> into its directory.</p>
  10. <p>Git helps the parent project precisely track the commit (not a symbolic <code>master</code> or <code>HEAD</code>) hash that the submodule is at for this parent project. This commit point can be different than another parent project that points to this submodule at a different commit point.</p>
  11. <h2>Updating Submodule Pointers</h2>
  12. <p>Start any submodule updating by executing <code>cd &lt;SUBMODULE&gt;</code> to navigate into the submodule directory.</p>
  13. <p>If there's new code to be pulled, it can be retrieved via <code>git fetch</code>. There's no need to merge it into a branch as submodules are meant to represent a point in time and a hash (sans branch or tag name) is exactly that -- a point in time.</p>
  14. <p>To visualize if all the new code has been retrieved, type <code>git log --all -5</code> top see the last five commits regardless if they are merged into any local branch. Copy the hash of the desired submodule snapshot.</p>
  15. <p>Next, <code>git checkout &lt;HASH&gt;</code> to the desired commit. The submodule's working directory now reflects the new snapshot state.</p>
  16. <p>Lastly, <code>cd ..</code> back to the master repository hosing the submodule. The <code>git status</code> command will show that the subdirectory has been modified. <code>git add &lt;SUBMODULE&gt;</code> will add the submodule's update HASH pointer to the staging area. Git commit will seal it into the master repository.</p>
  17. <h2>Restoring Submodules</h2>
  18. <p>To check out a project and set up the submodules:</p>
  19. <pre><code>git clone &lt;git://url/yourrepo.git&gt;
  20. cd &lt;yourrepo&gt;
  21. # Setup the submodule .gitmodules file
  22. git submodule init
  23. # Retrieve the submodules' code
  24. git submodule update
  25. </code></pre>
  26. <p>If another developer changes the submodule, then commits the parent project and you pull and retrieve that, you'll need to update the submodules again to stop your <code>git status</code> from telling you the tree is dirty.</p>
  27. <pre><code>git submodule update
  28. </code></pre>
  29. <p>The main concept is just to put the submodule in whatever state you want it to be in, then add and commit from the parent project. If a colleague does that and then you pull the parent project, you need to <code>update</code> to get the latest submodule code.</p>
  30. <h2>Coding in Submodules</h2>
  31. <p>Remember that submodule code changes need to be committed and pushed from within the submodule repository. In fact, it is better if submodule coding happens from a separate clone of the repository. If a submodule-coding developer fails to push updated code to the team-accessible host for the submodule code, her colleagues won't be able to retrieve the commits; the parent project will be pointing to a submodule hash that isn't publicly available.</p>