Pro Yearly is on sale from $80 to $50! »

Submodules Without Fear

F28c4835c9e2277b6e04b86574532a2d?s=47 Abizer Nasir
September 26, 2013

Submodules Without Fear

NSLondon Talk about using Git Submodules.


Abizer Nasir

September 26, 2013


  1. Nslondon - 26Th September 2013 Submodules Without Fear Abizer Nasir

    | @abizern | |
  2. Treasure Of The Sierra Madre, Warner Bros 1948 CocoaPods? We

    don’t need no steenkin’ CocoaPods!
  3. Adding Code To A Repository ✤ Add the files directly!

    ✤ Use a dependency mangagement system! ✤ CocoaPods! ✤ Git Submodules
  4. What is a Git Submodule? ✤ A Git repository that

    is added as a subdirectory of another Git repository.! ✤ The contents of the subdirectory are not tracked by the containing repository. ! ✤ It is checked out at a particular commit, not any particular branch.! ✤ The submodule communicates with it’s own upstream repository independently of the containing repository.
  5. Where is it? ✤ The .gitmodules file has a reference

    to the URL of the submodule and the directory which contains it. Shared with all repositories! ✤ The .git/config file has a reference to the URL of the submodule and directory which contains it. Local to the current repository.! ✤ The .git/modules folder has the actual git repository for the submodule! ✤ The sha of the commit is stored in the repository under the tree of the directory. Just like any other object sha.
  6. The Git Object Model Understand this and you’ll never be

    scared of Git again.
  7. 4 Basic Objects ✤ Blob - The individual files! ✤

    Tree - Directories! ✤ Commit - Pointer to a top level directory! ✤ Tag - An object in it’s own right, though usually the lightweight version is used which is like a branch.
  8. The blob ✤ The immutable building block of the object

    model! ✤ Just the contents of the file, not the name, or the mode! ✤ Compressed, and named after it’s SHA-1 hash! ✤ If the contents of several files are the same, the blobs are the same.! ✤ Referred to by the 40 character hash, though 4-7 characters are usually enough! ✤ git cat-file -p <sha> is a handy command
  9. The Tree ✤ Recursive object that just holds trees and

    blobs! ✤ Think of it as being like a directory! ✤ The tree is what holds the names and modes of the blobs and trees it contains.! ✤ git ls-tree <sha> is a handy command
  10. Todo example

  11. Todo example

  12. The commit ✤ This object references the top level tree.!

    ✤ Contains other information! ✤ Author / committer! ✤ date! ✤ commit title and message! ✤ parent commit(s)
  13. Todo example

  14. todo example

  15. The commit ✤ Blobs are immutable, so any change propagates

    upwards! ✤ Renames don’t change the file blob, because contents are the same, but the tree is changed! ✤ Because objects are immutable you never lose history as long as the commit is rooted
  16. Todo example

  17. Adding a Submodule (Example) git submodule add AFNetworking.git Externals/AFNetworking

  18. Adding a Submodule (Example) git submodule add AFNetworking.git Externals/AFNetworking

  19. Adding a Submodule (Example) git submodule add AFNetworking.git Externals/AFNetworking

  20. Creates An entry in .gitmodules [submodule "Externals/AFNetworking"]! ! path =

    Externals/AFNetworking! ! url =
  21. Creates An entry in .git/config [submodule "Externals/AFNetworking"]! ! path =

    Externals/AFNetworking! ! url = [submodule "Externals/AFNetworking"]! ! url =
  22. Adds a reference to the submodule commit [submodule "Externals/AFNetworking"]! !

    path = Externals/AFNetworking! ! url = [submodule "Externals/AFNetworking"]! ! url = • The index is updated, but the changes are not checked in.! • You can change the URL of the submodule locally.! • You can change the commit that the submodule points to.
  23. Submodule sha is stored after commit $ git ls-tree master:Externals!

    160000 commit 259c2c24aff803622f19aefe5c5a4c5772cc2f80! AFNetworking!
  24. Cloning a Repository With Submodules - Easy ✤ git clone

    --recursive <repo-url> - easiest way
  25. Cloning a Repository With Submodules - Longer ✤ git submodule

    init! ✤ Adds the url from .gitmodules to .git/config! ✤ git submodule update! ✤ Actually clones the submodule’s repository to the stated location
  26. git submodule status is your friend ✤ Shows the state

    of the submodules, and their description! ✤ If there are unchecked in submodules prepended with a ‘+’! ✤ If the submodule is not initialised prepended with a ‘-’
  27. Handy Tips ✤ Keep the submodule current with git submodule

    update! ✤ Check your submodules are up to date with git submodule status! ✤ ALWAYS push changes to your submodule before commiting to the enclosing repository.! ✤ git rm --cached files if you are extracting code to a submodule.! ✤ Use your own clone if you are adding Open Source! ✤ Use http:// URLs instead of git@ or git://
  28. Disadvantages It’s Git, innit?

  29. Disadvantages ✤ They aren’t called sob-modules for nothing.! ✤ You

    need to add the files to the IDE manually.! ✤ You need to link to system frameworks manually. ! ✤ Not as easy as just dropping the code into the repository or using CocoaPods.
  30. Advantages It’s Git, innit?

  31. Advantages ✤ You can choose whatever commit you want to

    use as a submodule.! ✤ Different branches can have different versions of submodules, or even different submodules.! ✤ Changes can be pushed up from the submodule to a shared repository! ✤ Less of a hurdle for pushing code back to Open Source Projects.
  32. Be Brave. Stay Calm You work on harder problems than

    this everyday.! ! You aren’t lazy with your code, why be lazy with your version control?
  33. Thank You Questions! or! Submodules With Beer!

  34. Now Go Look These Up. ✤ git submodule add! ✤

    git submodule init! ✤ git submodule update! ✤ git submodule sync! ✤ git submodule status! ✤ git submodule deinit! ✤ git rm