This tutorial will be about Git integration with Xcode.  For more in depth discussion of Git consult Pro Git or Version Control with Git

There are two different ways to put your project under Git source control.  The first is through the terminal with the git init command, and the second and much easier is to select the “Create local git repository for this project” checkbox in Xcode when creating a new project.

Open Xcode and create a new project File->New->Project.  Under Mac OS X select application and then select Command Line Tool.  Click Next, name your product GitTutorial.  Type should be foundation.  Click Next, make sure the checkbox at the bottom of the dialog is selected, and then click Create.

Open main.m and delete everything that is between the {} after @autorelease.  Instead, type in NSLog(@”This is a Git tutorial”);  Save the changes with File->Save.  After the save, an icon with the letter M shows next to main.m file.  This means that the file has been modified and at this point you can commit the changes to Git.  If you add a new file to your project an icon with the letter A shows up next to your new file.  This means that a new file was added and it needs to be committed to Git.  You don’t have to commit each modified or added file separately.  You can commit more than one file at the same time.  To commit the changes to Git, go to File->Source Control->Commit

On the follow up dialog you can see what changes will be committed and you should enter a commit message.  Click on the commit button to finish the commit.  If you want to view all the repositories managed by Xcode and all the changes made to them, open the organizer (Window->Organizer) or click on the organizer icon which is on the top right in Xcode.  After the organizer is opened, click on Repositories and on the left hand-side select GitTutorial repository.  You can view all the changes ever committed to Git.

Time to create a branch.  In the Organizer select the Branches for the GitTutorial repository.

There is only one branch at the moment the “master” branch.  Click on the Add Branch at the bottom.  Type in the new branch name testNewMethod and leave starting point to master.  Click on the Automatically switch to this branch checkbox.  Finally, click on create.

Add something new to main.m and then commit it to the new branch.  If we want to merge the changes to the master branch, we have to switch to the master branch first and then merge the changes.

To switch branches, select GitTutorial folder in the organizer.

After that click on Switch Branch and select the branch you want to switch to, in our case its master.

If you open main.m, you will see that the changes that you committed are not there any more.  We have to merge to changes to the master branch.  In Xcode go to File->Source Control->Merge.

And then select the branch that we want to merge from.  In our case we have only one other branch testNewMethod, just click on Choose.

After this a new dialog will show you what changes will be applied to your code.  Click on merge to complete the merge operation, and see how your code was updated with the changes you made previously.  It would also ask you if you want Xcode to take a snapshot of your project before the merge in case something goes wrong.  It is a good idea to do this for more complicated merges, but it is not necessary in our case.

Enjoyed this post?

Subscribe to our RSS Feed or Follow us on twitter to keep up to date with the latest from iOS-Blog. Remember, Sharing is caring so please click one of the following options:

Tags: , , , , ,