Dependency Management Using Git Submodules

In this Dependency Management tutorial you’ll learn how to use Git Submodules to manage both internal and external dependencies for your project. By Andy Obusek.

Leave a rating/review
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Removing A Dependency

To remove a Git Submodule dependency, first add ReactiveSwift as a dependency. Execute git submodule add https://github.com/ReactiveCocoa/ReactiveSwift.git Frameworks/External/ReactiveSwift:

PhotoTagger|master ⇒ git submodule add https://github.com/ReactiveCocoa/ReactiveSwift.git Frameworks/External/ReactiveSwift
Cloning into '/Users/andyo/Documents/AndyRW/PhotoTagger/Frameworks/External/ReactiveSwift'...
remote: Counting objects: 42067, done.
remote: Compressing objects: 100% (37/37), done.
remote: Total 42067 (delta 14), reused 0 (delta 0), pack-reused 42028
Receiving objects: 100% (42067/42067), 15.24 MiB | 5.37 MiB/s, done.
Resolving deltas: 100% (25836/25836), done.
PhotoTagger|master⚡ ⇒ 

Now you’ve added ReactiveSwift as a dependency. You can verify this by listing the contents of the folder where it resides by executing ls Frameworks/External/ReactiveSwift :

PhotoTagger|master⚡ ⇒ ls Frameworks/External/ReactiveSwift
CONTRIBUTING.md
Cartfile
Cartfile.private
Cartfile.resolved
Carthage
CodeOfConduct.md
Documentation
LICENSE.md
Logo
Package.swift
README.md
ReactiveSwift-UIExamples.playground
ReactiveSwift.playground
ReactiveSwift.podspec
ReactiveSwift.xcodeproj
ReactiveSwift.xcworkspace
Sources
Tests
script
PhotoTagger|master⚡ ⇒ 

To properly remove the dependency after it’s been committed, you’ll need to commit the dependency. Once again, execute git add . followed by git commit -m "Add ReactiveSwift dependency":

PhotoTagger|master⚡ ⇒ git add .
PhotoTagger|master⚡ ⇒ git commit -m "Add ReactiveSwift"
[master ebb1a7c] Add ReactiveSwift
 2 files changed, 4 insertions(+)
 create mode 160000 Frameworks/External/ReactiveSwift

Now that ReactiveSwift was added as a dependency, you’re going to remove it. To remove it, type: git rm Frameworks/External/ReactiveSwift:

PhotoTagger|master ⇒ git rm Frameworks/External/ReactiveSwift
rm 'Frameworks/External/ReactiveSwift'
PhotoTagger|master⚡ ⇒ 

This marks ReactiveSwift to be entirely removed from your local repository and filesystem. At this point, the changes need to be committed. Execute git commit -m "Remove ReactiveSwift":

PhotoTagger|master⚡ ⇒ git commit -m "Remove ReactiveSwift"
[master 557bab4] Remove ReactiveSwift
 2 files changed, 4 deletions(-)
 delete mode 160000 Frameworks/External/ReactiveSwift
PhotoTagger|master ⇒ 

And boom, it’s gone!

Wiring It All Up

You’ll need a bit of additional code before you can tag images in your app. Rather than copy and paste a bunch of code without much explanation, the final section of this tutorial provides a wired-up solution for you. You’ll just need a secret token from the Imagga API — read on to learn how to get one.

The Imagga API

You might recognize this API from our Alamofire Tutorial: Getting Started.

Imagga is an image recognition Platform-as-a-Service that provides image tagging APIs for developers and businesses to build scalable, image-intensive cloud apps. You can play around with a demo of their auto-tagging service here.

You’ll need to create a free developer account with Imagga for this tutorial. Imagga requires an authorization header in each HTTP request, so only people with an account can use their services.

Go to https://imagga.com/auth/signup/hacker and fill out the form. After you create your account, check out the dashboard:

alamofire tutorial

Listed down in the Authorization section is your secret token. Copy it into the clipboard.

Note: Make sure you copy the whole secret token. Scroll over to the right and verify you copied everything.

In the final project, open ImaggaRouter.swift and use your secret token as the value for authenticationToken.

Where to Go From Here?

Normally a final, completed version of the tutorial project is made available to you for download. Since this tutorial is made up of two projects connected via a Git Submodule, it seemed more fitting to provide the final project via a Git remote on github.com.

In addition, there’s one common task left to be explained that goes right along with this: cloning a repository that has a submodule dependency.

Bonus: Cloning A Repository With Submodules

To access the final and completed compilation of ImaggaRouter and PhotoTagger, you’ll clone a remote repository where they are stored. To do this, execute git clone --recursive https://github.com/raywenderlich/PhotoTagger.git:

temp|⇒ git clone --recursive https://github.com/raywenderlich/PhotoTagger.git
Cloning into 'PhotoTagger'...
remote: Counting objects: 40, done.
remote: Compressing objects: 100% (24/24), done.
remote: Total 40 (delta 11), reused 40 (delta 11), pack-reused 0
Unpacking objects: 100% (40/40), done.
Submodule 'Frameworks/External/Alamofire' (https://github.com/Alamofire/Alamofire.git) registered for path 'Frameworks/External/Alamofire'
Submodule 'Frameworks/Internal/ImaggaRouter' (https://github.com/obuseme/ImaggaRouter.git) registered for path 'Frameworks/Internal/ImaggaRouter'
Cloning into '/Users/andyo/Downloads/temp/temp/PhotoTagger/Frameworks/External/Alamofire'...
Cloning into '/Users/andyo/Downloads/temp/temp/PhotoTagger/Frameworks/Internal/ImaggaRouter'...
Submodule path 'Frameworks/External/Alamofire': checked out 'c9c9d091b308a57ff9a744be4f2537ac9c5b4c0b'
Submodule path 'Frameworks/Internal/ImaggaRouter': checked out 'ceb7415e46829c8a732fdd084b42d95c2f453fa2'
Submodule 'Frameworks/External/Alamofire' (https://github.com/Alamofire/Alamofire.git) registered for path 'Frameworks/Internal/ImaggaRouter/Frameworks/External/Alamofire'
Cloning into '/Users/andyo/Downloads/temp/temp/PhotoTagger/Frameworks/Internal/ImaggaRouter/Frameworks/External/Alamofire'...
Submodule path 'Frameworks/Internal/ImaggaRouter/Frameworks/External/Alamofire': checked out 'c9c9d091b308a57ff9a744be4f2537ac9c5b4c0b'
temp|⇒ 

The --recursive flag on the normal git clone command ensures all submodules are cloned at the same time. You can see in the output Alamofire and ImaggaRouter are also cloned. By default, this doesn’t happen with git clone.

To try these out together, you’ll need to connect the ImaggaRouter project as a dependency of the PhotoTagger project, and add your own secret token for the Imagga API.

For further reading, check out the following:

I hope you enjoyed this tutorial, and if you have any questions or comments, please join the forum discussion below!