3888081967_39e834a485_z

12 steps to using GitHub with XCode 4

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

  1. Launch XCode 4
  2. Select the ‘Connect to a repository’ option from the ‘Welcome to XCode’ dialog.
  3. Paste in your GitHub SSH URL in the Location text box. This can be found from your GitHub repository’s Source page. e.g. git@github.com:<organisation>/<repository>.git
  4. Click Next and then give the repository a name (e.g. RepositoryName) and click Clone.
  5. Pick a directory to clone your GitHub repository into (even if your repository is currently empty).
  6. 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.
  7. 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.
  8. Create a New Project within the directory you just cloned into but do not tick the ‘Create local git repository for this project‘ option.
  9. 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’.
  10. 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).
  11. 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‘.
  12. 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.

19 thoughts on “12 steps to using GitHub with XCode 4”

  1. Hi, thanks for sharing!

    I have exactly the same problem. The first time I push, Xcode always complains about wrong username/password. Hope they will announce fix during WWDC!

    Bill

  2. There is a new Mac client for github now – I’m still trying to get it integrated in fully with xcode, but for now it seems to work, except that syncing changes from github results in an update to the project file, requiring me to resync that up when I next want to pull more changes down. Any suggestions on fixing this?

  3. The new Mac client for github is excellent and I’m using it more than the built in support in XCode 4 now until they improve that experience. Make sure your .gitignore has the following entries and this should prevent unnecessary changes being detected by Git:
    .DS_Store
    build/
    *.pbxuser
    *.mode1v3
    *.mode2v3
    *.perspectivev3
    profile
    xcuserdata

  4. I set up a repository on Github for an already existing project, and initially Xcode was communicating fine with it. But now, whenever I restart Xcode, I get a “cannot connect to server” message, and the repository has a yellow light in Organizer.

    I am admittedly not well versed in Git. I’ve walked through the help section in Github a couple of times, but nothing seems to correlate with what I’m seeing. Do you have any insights?

  5. I downloaded the Github client for Mac and I seem to have kludged together a way for getting it all to work.

    I don’t understand why it has to be a two-step process, though. I can now “commit” my changes to the local version of Git. But then I have to re-commit from the client in order to get it all pushed up to Github. Why won’t Xcode just commit straight to Github?

  6. This is somewhat related, and still on topic about dealing with remote and local repositories with xCode / keeping them in sync…

    To get around some short comings with Git remote repositories & xCode using the MAC terminal and executing the simple git commands to add, push, commit, etc work nicely too.

    For example if you have an issue committing changes in a new folder in your local repository to the remote repository…

    1 – Close xCode
    2 – Open your terminal and in your terminal type:
    cd /git/repository/
    git add newfolder

    Open xCode and try to commit again… should work. Hopes this helps some ppl out there.
    Type ‘man git’ without the quotes in your MAC terminal to see all the Git Command Line options.

  7. Thanks for sharing. It helps a lot. I’m using XCode 4.2.1. It seems like after cloning a repository, if the repo contains an XCode project this version could open that project and commit/push normally.

    And I wonder in Jonathan’s reply should
    xcuserdata
    be
    xcuserdata/
    (a folder?) because with only xcuserdata whenever I tried to push XCode said there is a file in xcuserdata/ modified but not commited.

  8. It seems to me this “connect to a repo” is a tool to help connect to an existing project, rather than helping create a new one.

  9. thanks so much! these steps were awesome. i tried with no avail to get the github mac app to work and finally, with your instructions, could use xcode 4 instead :)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>