A few years ago I wrote a series of posts about getting Subversion working with Xcode. Now that the latest versions of Xcode now support Git, I figured I would put together a tutorial about getting Git working with Xcode. Having worked with Git for only a short while, I find it is a much easier source control management system to work with compared to Subversion and others.
Getting Started with Git
If you are brand new to Git, you can get a quick overview of the product and download it by visiting the Git official website. One of the best overviews and tutorials that I have found is at the Git Reference website. The tutorial walks you through how Git was conceived and introduces you to some of the most common commands.
Creating a Local Git Repository for NEW Xcode Projects
The easiest way to put a new Xcode project under version control within Git is to select the Create local git repository for this project check box when creating your project in Xcode. You can verify that this project is under version control by looking for the .git directory under the project. You will need to use Terminal to browse to your project folder and perform a ls -a to see the directory.
Creating a Local Git Repository for Existing Xcode Projects
To add an existing project to a new local Git repository will require you to use the command line. Open Terminal and navigate to your project directory. You should first create a .gitignore file in your project directory. This file will tell Git to not track certain file patterns.
Here is a sample .gitignore file for Xcode projects:
If you walked-through the tutorial I mentioned above in Getting Started with Git, these commands should be familiar to you.
1. Initialize a new Git repository
2. Add project files to Git repository
3. Perform an initial commit of files to Git repository
4. Add repository to Xcode
Your code should now be under version control; however, now we need to tell Xcode about the local repository we just created. Open Xcode and from the Window menu, open the Organizer.
The Organizer lists all registered devices, projects, snapshots, documentation and version control repositories. Click on the Repositories icon.
You need to create a new repository, click on the plus (+) in the bottom left of the window and select Add Repository…
In the Add a Repository wizard, enter the following:
- 1. Name – This is typically the name of the project or collection of projects.
- 2. Location – Since we are adding a local repository, this the absolute path of your local repository or project.
- 3. Type – This should be Git in this situation.
After you click Next, your local repository should be listed on the left-hand side of the screen. Clicking on it, should list an entry for your initial (or subsequent) commits and the files modified.
You should now be able to make changes to your project within Xcode and commit your changes using the Xcode GUI. In the follow up posts to this series, I will walk you through using the Xcode GUI to commit changes and also show you how to setup a remote repository on BitBucket.