About Version Control
What is version control, and why should you care? Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later. If you are a developer and want to keep every version of your source code a Version Control System is a very wise thing to use. It allows you to add features to your app without disturbing the original source code. It also allows you to revert files back to a previous state, revert the entire project back to a previous state, compare changes over time and much else.
Xcode 4 introduced version control into the Xcode environment. Initially, it supported only local Git repositories, but Xcode 4.2 has brought remote repositories to the environment. This is great news if you are part of a development team or if you work on multiple machines. Remote repositories also provide a safe place for your code in case of computer failure or loss.
Creating a Local Repository
Create a new project, select the Single View Application template.
Enter the following Settings
- Product Name: versionControl
- Devices : iPhone
- Select "Use Automatic Reference Counting"
You are given a option to create a local Git repository. If you select this box, Xcode will create the local repository.
As you modify and create new files, their source control status will be displayed in the navigator window. "A" is for a file that is been added to your project, where "M" means a modification to a file. Go to ViewController.m and modify the ViewDidLoad method like this:
Add a new file to the project with File ➤ New File. Choose iOS ➤ Other ➤ Empty. Name this file emptyFile. Now you can see the modified and added files in the Project Navigator.
To commit your changes back into your local repository, go to File ➤ Source Control ➤ Commit. The Commit window will be presented. By clicking a modified file, you will see your edited version in the left pane and the current version in the repository inthe right pane. All of your changes will be highlighted so that you can easily see the differences between the two files. At the bottom you can enter a message about the changes you have made before commit.
Branches and Merges
Branches are copies of your project that you can work on without disturbing the mater branch. They allow you to add features without affecting the main build. To manage your repository, you can go to Window Organizer and click the Repositories tab. In this view, you will see a list of repositories. You can click the Branches folder under a repository to see a list of branches available for this repository.
When you select a branch, you will see a list of the latest commits to that branch. The information includes who made the commit and the commit message. When you want to add a feature to your app, you can create a new branch by clicking the "Add branch button". Type a branch name and click the check box next to "Automatically switch to this branch". This will copy your code from the master branch into this new branch and then it will switch you to this branch.
Now make some additions to your code and when you are done you can commit this back to the repository. This will only affect the newly created branch. To switch back to the master branch, and click the "Switch Branch" button. Select the master branch and click OK.
Go back to the Xcode window and click File ➤ Source Control ➤ Merge. Then select which branch you want to merge into the current branch (master), When you click Choose, you will see the commit changes window.
Clicking the Merge button will combine the current branch into the master branch. If you look at the master branches commit history in the Organizer view , you will see that the commits from the new branch have been combined with the master branch commits.
Up until now, you have been working with local repositories. in Xcode you have support for remote repositories. This allows you to store your code online where you can get to it from any computer and where you can share your code woth others. To enable remote repositories you should create a acoount at www.github.com and follow the instructions to create a repository. Once you’ve created a repository on GitHub, copy the entire HTTP access path on the Source tab.This will be the remote location of your repository.
To add a remote repository, go to the Organizer view on the Repositories tab and select the repository you want to add a remote option to. Click the Remotes folder under the project repository, and click Add Remote at the bottom. Enter a name for your remote repository and the location, and then click Create.
Now that you have a remote repository, you can push and pull your code from the remote repository to keep things up to date with File ➤ Source Control ➤ Push. Select the GitHub repository, and click Push. Xcode will now send your code to your GitHub repository.