Switch Git Branches for Your iOS Project Easily and Safely
August 16, 2015
First the backstory.
Call2Disciple was my first iOS app, and to this day is the most complex app I work on. I take advantage of Git branches to implement any new features; these branches are isolated from the master branch, which I try to keep as stable as possible. Thus, only fully completed and stable features go into master, and then out to the App Store. I also put the Pods/
directory in the .gitignore
as it was giving me a load of trouble before, and it saves unnecessary and tedious commit work.
A while back I began experiencing weird build and linking errors in building the Call2Disciple app target. These errors were very cryptic and it took multiple Google and Stack Overflow searches to ultimately run pod install
, clean the project, and nuke the derived data; and the errors were fixed.
Everything seemed fine after that, until I wanted to work on a different branch. It seemed that every time I switched branches the same build and linking errors would pop up, and I would have to repeat the same process to fix it. After a few branch switches and subsequent troubleshooting sessions it became very tedious to have to fix these errors every single time I switched a branch. So I thought, Why not automate this process on branch switch? So I did.
It turns out, Swift is a great scripting language. I was able to, with some help from some gists to get me programmatic access to the command line, create a simple script to:
- Switch to the specified branch
- Run
pod install
- Delete the Derived Data folder
- Clean the project
It's very bare-bones, but it gets the job done.
So, I've in turn posted the code as a gist. It includes places in the code that you need to customize to fit your own needs. The code that does most of the work is towards the bottom; A lot of it is code to access the command line.
Enjoy.