The Git integration in XCode 4 is very welcome however it seems to be a bit flaky when it comes to dealing with remote repositories. For clarity I’m using Mac OS X version 10.6.7 and XCode 4.0.2 (4A2002a).
If I create a new XCode 4 Project and accept the option to create a local Git repository, all is well. I can commit locally and that’s great.
When I want to push the new project to GitHub I want to add a remote repository and push my local changes up to GitHub. I can add a GitHub repository to XCode 4 as a new Repository in the Organizer window but there’s no method of setting it as a remote repository for my existing project.
So instead here’s the 12 steps that I followed to get GitHub to work with a new XCode 4 project
- Launch XCode 4
- Select the ‘Connect to a repository’ option from the ‘Welcome to XCode’ dialog.
- Paste in your GitHub SSH URL in the Location text box. This can be found from your GitHub repository’s Source page. e.g. email@example.com:<organisation>/<repository>.git
- Click Next and then give the repository a name (e.g. RepositoryName) and click Clone.
- Pick a directory to clone your GitHub repository into (even if your repository is currently empty).
- The 1st attempt appears to fail even though contents are downloaded. Click ‘Try Again‘ and XCode 4 is happy and shows a green tick icon.
- Close the dialog. There is now a new Repository defined in the Organizer window (Shift+Cmd+2) with its origin set to the GitHub remote SSH URL.
- Create a New Project within the directory you just cloned into but do not tick the ‘Create local git repository for this project‘ option.
- Do an initial commit to the local Git repository. You can either choose File -> Source Control -> Commit or press Option+Cmd+C. Enter a commit message and click ‘Commit’.
- Now try to push your local changes up to GitHub. From the application menu, select File -> Source Control -> Push. XCode 4 fails with ‘The operation could not be performed because access to the repository <RepositoryName> was denied. Check username and password‘ (where RepositoryName is the name you entered earlier).
- Try pushing again by selecting File -> Source Control -> Push and XCode 4 pops up the remote Repository dialog again. This time it has detected a remote Repository called origin. Next to it there’s a yellow ball and the message ‘Authentication required‘.
- Clicking ‘Push’ again causes the remote Push to work.
As XCode 4 has created a new Repository called ‘origin’ to match the remote name, I’m not sure what’ll happen if you need to interact with more than one GitHub remote repository.
Another thing I’ve spotted is that the response messages back from GitHub with the checkin hash value and status message get reported in the XCode 4 Log navigator (Cmd+7) as Push failures, even though the Push is successful as far as GitHub is concerned.
I’m hoping that XCode 4 will have some patch work done to help improve this interaction with remote repositories in the near future. Apple are spot-on for encouraging users to check-in their work, it’d just be nicer to see a more collaborative focus on features rather than maybe assuming most iOS developers are sat on their own.