Book Image

Swift Cookbook

Book Image

Swift Cookbook

Overview of this book

Table of Contents (18 chapters)
Swift Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Adding a control version system to an existing project


It's very common starting a project without any version control, and with the passage of time, we change our idea and decide to add one.

Unfortunately, Xcode doesn't give this option to us and we have to do by hand. I hope this option will be added on Xcode soon.

Getting ready

To do this recipe, let's create an empty project called Chapter1 Git; however, this time before we save the project, uncheck the option Create Git CGRepository on.

How to do it...

Follow these steps for creating a local repository on an existing project:

  1. Open a Finder window again and use the shortcut command + Shift + U, or open the utilities folder from your Launchpad. Now, open the terminal and go to your project folder.

  2. Change your folder using the terminal by typing cd followed by a white space. Now, without closing your terminal, open the Finder window, then go to your project folder, drag the folder from the title bar to your terminal window, and then press the Enter key.

  3. Now, just type the following commands:

    git init
    git add .
    git commit -m "Initial commit"
  4. Now, you can open your project and notice that Xcode already recognizes the VCS. If you want to be sure of it, modify a file, save it with command + S and check that you have the letter M on the right side of your file on the project navigator.

  5. Once you are happy with your changes, you can deliver them by right-clicking on AppDelegate.swift, then go to the Source Control option and select Commit AppDelegate.swift.

  6. Then, a dialog asking for a description will appear, write about your modification as comment, and click on commit 1 file.

How it works...

Unfortunately, if you forgot to add a Git repository to your project, Xcode doesn't provide any mechanism to add it to your project, so you have to add it by hand. Opening the command line allows you to use Git from the command line and Xcode detects that this feature has been added. Some versions of Xcode can only detect that the version control has been added when you open your project, so if you've done all steps and Xcode hasn't detected it, try closing and opening Xcode again.

Tip

Xcode offers you some features to work with Git and SVN, but they are very limited. If you need more commands from your VCS, you can use them from the command line, or use an external tool for it.

There's more...

Even if you are not going to work as part of a team, I recommend that you use a version control system. When developing with Swift or other languages, you sometimes need to rollback or compare the current code with the previous versions of it, mainly when you have a new bug.

If you would like to know more on this topic, check out the book Git Version Control Cookbook, by Packt Publishing.