Slide 1

Slide 1 text

Nslondon - 26Th September 2013 Submodules Without Fear Abizer Nasir | @abizern | abizern.org | 365git.tumblr.com

Slide 2

Slide 2 text

Treasure Of The Sierra Madre, Warner Bros 1948 CocoaPods? We don’t need no steenkin’ CocoaPods!

Slide 3

Slide 3 text

Adding Code To A Repository ✤ Add the files directly! ✤ Use a dependency mangagement system! ✤ CocoaPods! ✤ Git Submodules

Slide 4

Slide 4 text

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.

Slide 5

Slide 5 text

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.

Slide 6

Slide 6 text

The Git Object Model Understand this and you’ll never be scared of Git again.

Slide 7

Slide 7 text

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.

Slide 8

Slide 8 text

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 is a handy command

Slide 9

Slide 9 text

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 is a handy command

Slide 10

Slide 10 text

Todo example

Slide 11

Slide 11 text

Todo example

Slide 12

Slide 12 text

The commit ✤ This object references the top level tree.! ✤ Contains other information! ✤ Author / committer! ✤ date! ✤ commit title and message! ✤ parent commit(s)

Slide 13

Slide 13 text

Todo example

Slide 14

Slide 14 text

todo example

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Todo example

Slide 17

Slide 17 text

Adding a Submodule (Example) git submodule add https://github.com/Abizern/ AFNetworking.git Externals/AFNetworking

Slide 18

Slide 18 text

Adding a Submodule (Example) git submodule add https://github.com/Abizern/ AFNetworking.git Externals/AFNetworking

Slide 19

Slide 19 text

Adding a Submodule (Example) git submodule add https://github.com/Abizern/ AFNetworking.git Externals/AFNetworking

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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.

Slide 23

Slide 23 text

Submodule sha is stored after commit $ git ls-tree master:Externals! 160000 commit 259c2c24aff803622f19aefe5c5a4c5772cc2f80! AFNetworking!

Slide 24

Slide 24 text

Cloning a Repository With Submodules - Easy ✤ git clone --recursive - easiest way

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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 ‘-’

Slide 27

Slide 27 text

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://

Slide 28

Slide 28 text

Disadvantages It’s Git, innit?

Slide 29

Slide 29 text

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.

Slide 30

Slide 30 text

Advantages It’s Git, innit?

Slide 31

Slide 31 text

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.

Slide 32

Slide 32 text

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?

Slide 33

Slide 33 text

Thank You Questions! or! Submodules With Beer!

Slide 34

Slide 34 text

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