Adding a git repository to an existing Xcode project

One would think the need to add a git repository after the creation of a new Xcode project wouldn’t come up too often, considering the option to place a new project under source control is presented whenever you create a project in Xcode. However, there could be instances of side projects, or a late night coding frenzy, turning into something bigger, and though source control was initially a little overkill, it would become paramount moving forward.

Hence this short tutorial on adding a git repository to an Xcode project that; a) already exists (i.e, has already been created); and b) doesn’t already have a git repository associated with it.

Here’s the things you need to have for this tutorial:

A )  Git must be installed on your Mac (you can download it by going here)

B )  An Xcode project that already exists, and does not already have a git repository

C )  Access to the Terminal application within your OS X user account

D )  The file pathway to your Xcode project (this should be the top level directory of the project, which is the same directory that contains the project’s “.xcodeproj” file)

The entirety of this endeavor takes place in the Terminal application. The first thing you’ll need to do is open the Terminal application. This is most easily done by performing a Spotlight search with the word “Terminal” as the query. Once you’ve launched the Terminal, you will need to enter several commands to create and initialize the git repository, and then perform the first commit to the newly minted repository.

Here are the steps:

1 )  You must first change directory to the root of the project you’re wanting to place under source control with to following command:

cd pathway/to/project

2 )  There is a file in each Xcode project directory that tracks the state of Xcode’s UI. This file is constantly being updated, so you don’t want it to be placed under source control. Just to be clear, this is not actually part of your project, but only a setting file that Xcode keeps around. To find the file, use the appropriately named Unix shell command “Find” and a period; the period is Unix shell syntax for “the current directory”, so the following command will show you all files in your project directory:

find .

3 )  The file you are looking for ends with the extension “.xcuserdata”. Highlight and copy the file pathway for the file, being careful to include the entire pathway *except* for the initial “./”, or period and forward slash.

4 )  In order to not place it under source code, you must create a file called “.gitignore” within the root directory of your project. In order to do this you use the “echo” command with the pathway of the “.xcuserdata” file:

echo pathway/to/UIstate.xcuserdata >.gitignore

5 )  Though it’s not required, it is a good idea to check the contents of the “.gitignore” file. You use the “cat” command to do this; the output for the “cat” command should be the file pathway to the “.xcuserdata” file:

cat .gitignore

A quick aside…

This section is entirely optional, but it’s recommended if you have just installed git for the first time, if you haven’t configured your contact information within git before now, or if you need to update this information. Here is how you set your username and email for git; this is useful because git uses this information when it tags commits to the repository.

git config –global “Your Name”

git config –global “”

6 )  Next, it is time for creating the repository. After entering the special git command “init”, you will get a confirmation line of text that says “Initialized empty Git repository in pathway/to/project”. The command is:

git init

7)  Now that we have a new, empty repository we need to add files to it! You do this with another git command called “add”, and then specify the pathway you want to add to the repository; note  again that the “.” (period) is Unix shell syntax for “the current directory”. Also, you will not receive any feedback that the files were added to the repository.

git add .

8 )  Lastly, you need to commit to the repository. This is also done with a git command. It is recommended to pass one argument along with the command to add a comment to the initial commit. One thing to note: when writing your comment, do not write an exclamation point in your comment, as the git command sees it as part of its syntax, and the commit command will fail.

git commit -m “Enter your comment here”

After the “git commit” command, all the files that were committed to the repository will be listed in the Terminal. Your project is now under source control!

Happy coding!

Jonathan DaleyXcode, tutorials