Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Submodules Without Fear

Abizer Nasir
September 26, 2013

Submodules Without Fear

NSLondon Talk about using Git Submodules.

Abizer Nasir

September 26, 2013
Tweet

More Decks by Abizer Nasir

Other Decks in Programming

Transcript

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

    | @abizern | abizern.org | 365git.tumblr.com
  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. 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.
  7. 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
  8. 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
  9. The commit ✤ This object references the top level tree.!

    ✤ Contains other information! ✤ Author / committer! ✤ date! ✤ commit title and message! ✤ parent commit(s)
  10. 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
  11. Creates An entry in .gitmodules [submodule "Externals/AFNetworking"]! ! path =

    Externals/AFNetworking! ! url = https://github.com/Abizern/AFNetworking.git
  12. Creates An entry in .git/config [submodule "Externals/AFNetworking"]! ! path =

    Externals/AFNetworking! ! url = https://github.com/Abizern/AFNetworking.git [submodule "Externals/AFNetworking"]! ! url = https://github.com/Abizern/AFNetworking.git
  13. Adds a reference to the submodule commit [submodule "Externals/AFNetworking"]! !

    path = Externals/AFNetworking! ! url = https://github.com/Abizern/AFNetworking.git [submodule "Externals/AFNetworking"]! ! url = https://github.com/Abizern/AFNetworking.git • 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.
  14. Submodule sha is stored after commit $ git ls-tree master:Externals!

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

    --recursive <repo-url> - easiest way
  16. 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
  17. 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 ‘-’
  18. 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://
  19. 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.
  20. 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.
  21. 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?
  22. 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