Using Git Submodules With Xcode — Tutorial For iOS

aestusLabs
3 min readJun 22, 2017

--

If you need to share code between your various Xcode projects git submodules may be right for you. I created boilerplate code for local notifications that I wanted to reuse. The solution I found was git submodules.

Note: This tutorial assumes you have a bit of experience with GitHub and Terminal.

Step 1 — Create A New GitHub Repo

This will be repository you put the files in. Next, upload the files to the repo. I just dragged in a few .swift files from my Xcode project folder in Finder.

Click ‘Clone or Download’ and copy the SSH key

Step 2 — Add Submodule To Your Project File

Note: I’d recommend a test project if this is your first time doing this.

Once your repo is set up go to terminal. Use ‘cd’ to navigate to your root project folder. (The folder that contains files like AppDelegate)

In terminal type (no ‘ marks) :

git submodule add 'yourGitHubReposSSHkey'

It may prompt you for your passphrase.

Step 3 — Drag The Files Into Your Xcode Project

Go to Finder and navigate to the folder that contains the files from your GitHub Repo. Drag these into your Xcode project.

It will present a window, these are what to select:

  • Copy Items If Needed (checked)
  • Create Folder References
  • Add To Your AppName Target (checked)

(It didn’t work when I tried to drag the folder in. I believe it was my ignorance of how Xcode works. The folder and files were in the project and I could edit them but couldn’t access them from a ViewController.)

That’s it!

Bonus

These files have there own source control. Tap on Source Control on the top bar and you will see:

Testing Adding Git Submodules is my Xcode project and ASNotifications is my git submodule. This is really cool. You can make changes to the files from your submodule and push them up to GitHub. You can also pull changes if you update the code from another app. Exactly what I wanted, one truth source for files I plan to share across almost every app I develop.

I am changed the code in one of my submodule files and am pushing it up to my repo.

Conclusion

Why rewrite boilerplate code over and over again. I plan to make a bunch of submodules and add them to my apps. For example, I am creating a number of health apps with a common UI. I made the UI progromatically so I can create a submodule and pull it into all my apps. A lot better than my original plan of copying and pasting files between projects.

If you are interested in my boiler plate local notifications code you can grab the repo here. Read the README if you do.

Like if this helped you out, subscribe if you want more posts like this. Every time I have a problem that takes more than a single source to solve I am going to make one of these posts. Also let me know how I can improve how this tutorial was written. Thanks

Tutorial by aestusLabs. Share your knowledge.

--

--

aestusLabs

I’m an aspiring software developer, focusing on iOS Swift development. When I solve a problem in my own app development I post a tutorial to help you out.