Git Submodules

NOTE: This document is now deprecated. iOS was the only discipline using submodules, and is now switching to CocoaPods for future projects.

This document will remain available for the purposes of maintaining older projects.

Submodules allow foreign repositories to be embedded within a dedicated subdirectory of the source tree, always pointed at a particular commit.
-- git-submodule(1) Manual Page

Basic Usage

This example uses AFNetworking as a Git submodule for the project.

Cloning Repos with Submodules

$ git clone --recursive <url>

Adding Submodules

In the main repo working directory:

$ mkdir Lib
$ git submodule add https://github.com/vokal/AFNetworking.git Lib/AFNetworking

Then commit the change.

Notes:

##Removing Submodules
Removing submodules is a manual process, frought with terror.

  1. Delete the relevant section from the .gitmodules file. It will look similar to this:

     [submodule "Lib/AFNetworking"]
             path = Lib/AFNetworking
             url = https://github.com/vokal/AFNetworking.git
    
  2. Delete the relevant section from the .git/config file. It will look similar to this:

     [submodule "Lib/AFNetworking"]
         url = https://github.com/vokal/AFNetworking.git
    
  3. Remove the directory from git's tracking. git rm --cached Lib/path/to/submodule (no trailing slash).

  4. Commit your changes.

  5. Delete the now untracked submodule files. rm -rf Lib/path/to/submodule

Updating Submodules

In the main repo working directory:

$ git submodule update --init --recursive

Changing Repo Addresses For Submodules

If you need to change the repo address for a submodule (for instance, if you initially cloned your submodule directly from a vendor repo instead of the Vokal fork), follow these steps:

  1. Delete your entire local repository. All the git files, all the project files. Everything. Nuke it from orbit, it's the only way to be sure.

  2. Go to your fork of the project repository on GitHub, and find the portion of the .gitsubmodules file where you are pointing at the incorrect repo (in this example, pointed directly at a Square repo):

     [submodule "Cyfr-iOS/Utilities/Vendor/SocketRocket"]
          path = Cyfr-iOS/Utilities/Vendor/SocketRocket
          url = https://github.com/square/SocketRocket.git
    
  3. Using the Edit button on GitHub, update the url to point to Vokal's fork of the submodule's repo:

      [submodule "Cyfr-iOS/Utilities/Vendor/SocketRocket"]
         path = Cyfr-iOS/Utilities/Vendor/SocketRocket
         url = https://github.com/vokal/SocketRocket.git
    
  4. Clone your updated repo from GitHub. The submodule will now be pointed at the Vokal fork.

Additional Information