Skip to content

Instantly share code, notes, and snippets.

@paul-matthews
Created March 8, 2012 10:31
Show Gist options
  • Save paul-matthews/2000280 to your computer and use it in GitHub Desktop.
Save paul-matthews/2000280 to your computer and use it in GitHub Desktop.
Git Submodule Slides - From PHPUK 2012

% Git Submodules - The Good + The Bad % Paul Matthews % March 2012

This talk

  • Submodules
  • Essentials
  • Using submodules
  • Using distributed submodules
  • Alternatives

Submodules

  • Include other git repos
    • Anywhere in the tree
  • Submodules != svn:externals
  • Only entire repos
  • Use in conjunction with symlinks (linux)

Submodules use cases

  • Include other git projects

  • Including OS projects, such as frameworks

  • Create a custom structure for your codebase

  • Deployment?


Essentials

Git isn't like SVN

  • Data structure like linked list
  • Everything is a reference
    • Branches
    • Tags

Git isn't like SVN

  • Commit can be multiple branches / tags
  • Branch HEAD is only branch

Using Submodules

  • Submodules refer to a commit
  • Look like a repo inside a repo
    • Inside the submodule you can't tell you're in submodule

Definition

  • Parent : Amazon
  • Child : Magento

Creating child

  • Do Nothing from child

Create parent

  • Just Add them
git submodule add <repo> <location>

Cloning parent

  • 2 stage process
  • Need to initialise the submodules
git submodule init
  • Then pull the submodules down
git submodule update
  • Alternatively use "--recursive" clone

Updating a submodule

  • Submodules don't update themselves
    • Good / Bad
  • Add the submodule like a file
  • Work-flow:
    • Work on child
    • Commit
    • Goto parent
    • Add child reference
    • Commit

Updating a submodule (demo)

  • Work on child
cd ~/amazon/magento
vim index.php
  • Commit
commit -am "Updating magento"
  • Goto parent
cd ~/amazon
  • Add child reference
add ~/amazon/magento
  • Commit
commit -m "Getting latest version of magento"

Advanced Submodules

  • Submodules don't support multiple remotes
    • by default
  • Can be used to checkout multiple repos
    • Into a specific state
  • Useful for distributing many repos to teams
  • User can check in an inaccessible commit
  • Utility command
git submodule foreach [command]
  • e.g:
git submodule foreach git branch

Submodules controlling features

  • Children contain feature branches
  • So too can the parent
    • Ensures correct libraries per feature
    • Ensures system state

Overcoming multiple remotes

  • Create an init script
    • Adds all the remotes
  • Beware:
    • HEAD commit of submodule must be accessible on default remote

Alternatives to submodules

  • Subtree Merging
    • More useful for frameworks and libraries
    • You commit the child code to your parent branch

Roundup

  • Submodules
  • Essentials
  • Using submodules
  • Using distributed submodules
  • Alternatives
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment