In Xcode 4 how do I add a remote GitHub repository to an existing local project?

64,924

Solution 1

I think I found the Xcode way to push a existing project to GitHub or any other remote repository!

Preconditions

  • I assume that you have a project created by Xcode 4 with a local git repository.
  • You created an empty remote git repository (I call it RemoteRepository in in the following example)

Push it to remote

  1. Open the Organizer in Xcode an go to the Repositories tab

  2. At first click on Remotes and then on "Add Remote"

    Xcode Organizer

  3. Enter a Remote Name and the Location of your remote repository and click on create

    Add a remote dialog

  4. Then select your project and open File -> Source Control -> Push ...

    open push dialog

  5. Select your remote repository and wait until it is connectable! Look for the Create in the brackets.

    push dialog

  6. Now your old project is published to GitHub

    result on GitHub

I hope it will works for you also! In my case it works with every remote git repository.

Please contact me on twitter if something unclear or wrote in bad English!

Solution 2

Xcode 6.2 (and possibly 6.x) changed things around. There no longer is a Repositories tab in the Organizer. Here's how to do it with the newer Xcodes.

To add a project with a local git repository to GitHub, this is what you do:

  1. Go to the Source Control menu, elect your project and select Configure:

enter image description here

  1. You'll see:

enter image description here

  1. Select the Remotes tab, and press the "+" in the lower left corner. Select Add Remote.

enter image description here

  1. Type in a name for the remote repository, and type in the URL for your remote repository. Make sure you've created this remote repository already, and if on GitHub, make are you specify that this is an existing project, and not a new one.

enter image description here

  1. You're almost done. Go to the Source Control menu and select Push. You will then see the name of the repo you used in the last step, something like "simplest/master"; press the Push button.

Done.

Solution 3

I've done this successfully using an ssh connection (which looks like [email protected]:<username>/<project>.git). It authenticates the user based on an SSH public key, which you can generate on the command line--lots of docs about that at github in the "help" section.

Share:
64,924
jbjon
Author by

jbjon

Solutions Architect at Commify; DevOps, Tech speaker, Elasticsearch fan, @CodeClub volunteer, @TwineRadio host. @BookClubPodShow member.

Updated on November 14, 2020

Comments

  • jbjon
    jbjon over 3 years

    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 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 I switch to using git commands in Terminal.

    git remote add origin https://<username>@github.com/<organization>/<repository>.git
    

    If I then go back to Xcode 4 and try to use

    File -> Source Control -> Push...

    then the Choose the repository to which to push changes. dialog appears listing the origin remote repository. There's a yellow indicator saying that Authentication is required, which is fine as I've not been able to enter my credentials yet for HTTPS. However when I click 'Push' and it tries to push the changes it says "The operation could not be performed because access to the repository 'origin' is denied."

    A dialog prompting me for my username and password then appears with my pre-filled out so it must be reading this correctly from the local repository's config.

    When I enter my password Xcode 4 then crashes with the following error

    ASSERTION FAILURE in /SourceCache/IDEKit/IDEKit-303/Framework/Classes/SourceControl/IDESourceControlOperationInfo.m:59
    Details:  Message sent to invalidated object: <IDESourceControlPushOperationInfo, 0x200bd2a80>. Backtrace for invalidation:
     (null)
    Object:   <IDESourceControlPushOperationInfo: 0x200bd2a80>
    Method:   -invalidate
    Thread:   <NSThread: 0x200020700>{name = (null), num = 1}
    Hints:   None
    

    If I restart Xcode an origin repository has been added to the Repositories browser in Organizer and I can see the master branch on GitHub so I know the credentials are right without me changing anything.

    Has anyone found a reliable way of using Xcode 4 with GitHub starting with a local project first?

    If I clone an existing GitHub repository with an Xcode project in it, open it with Xcode 4 then even though the first Push seems to fail, the 2nd push did then work. So maybe starting from absolute scratch with Xcode 4 creating the repository for me isn't the best pattern (until they improve the Git tooling in a later version of Xcode 4).

    EDIT WITH FURTHER DETAILS OF ONE WORKAROUND

    This example only really applies to the first project you're adding to a GitHub repository. As such I think you're better off with the following steps, bearing in mind there still seem to be some Xcode 'gotchas' in this solution.

    1. List item
    2. 'Connect to a repository' from Welcome to Xcode dialog.
    3. Use GitHub SSH URL for Location
    4. Give it a name (RepositoryName)and Clone to a directory
    5. 1st attempt appears to fail even though contents are downloaded
    6. Try Again and it works.
    7. There is now a new Repository defined in the Organizer window 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 initial commit to local Git repository.
    10. Try Source Control -> Push and Xcode 4 fails with 'The operation could not be performed because access to the repository RepositoryName was denied. Check username and password'
    11. Try Source Control -> Push and Xcode 4 pops up the remote Repository dialog again but this time has detected a remote Repository called origin. Next to it there's a yellow ball and 'Authentication required'.
    12. Clicking 'Push' again caused the remote Push to work.

    One thing I've spotted and not managed to work out yet is that the response messages back from GitHub with the checkin hash value and any messages seem to get reported in Xcode Log navigator as Push failures, even though the Push is successful as far as GitHub is concerned.

    Anyone else have a better solution?

  • jbjon
    jbjon about 13 years
    Ok I'll try giving the SSH access method another go then. Like the HTTPS, I can add an ssh repository fine into the Organizer window but it seems to be at the point of interacting with the remote repository that it fails to authenticate properly. Thanks for letting me know you've had it working ok. Gives me some hope!
  • jbjon
    jbjon about 13 years
    I've written a blog about my experience getting GitHub to work with XCode 4 on new projects using the SSH method. Would appreciate your thoughts on what I found, Dan. mindthe.net/devices/2011/04/28/…
  • robhasacamera
    robhasacamera about 12 years
    Thank you, this is the only solution that worked for me since I had an existing project.
  • Luca
    Luca about 12 years
    @Tim Krueger: Hi Tim, tahnx for your clarifications:), i am wondering if a i can publish my project on GitHub to be visible only by team members while we are developing it, so to make GitHub a shared place for my team. Thanx in advance
  • Tim
    Tim almost 12 years
    @Luca Is your comment a question? Sure is this possible. But you must pay for private repositories on GitHub!
  • Nicolas Miari
    Nicolas Miari almost 12 years
    I successfully pushed through terminal. But when I tried from the Organizer, I got the dreaded "command not found" when inspecting the remote, and when trying to push, the remote appears as 'off line'. It is located on a Mac Os X server on my local network...
  • Teofilo Israel Vizcaino Rodrig
    Teofilo Israel Vizcaino Rodrig almost 12 years
    Why not to use BitBucket instead?. It supports Git.
  • Tim
    Tim almost 12 years
    @TeofiloIsraelVizcainoRodrig The problem was not the repository provider! My description should work also with BitBucket.
  • Teofilo Israel Vizcaino Rodrig
    Teofilo Israel Vizcaino Rodrig almost 12 years
    @Tim Krueger I mean that in BitBucket you can have private repositories without to pay
  • Tim
    Tim almost 12 years
    @TeofiloIsraelVizcainoRodrig Sorry! I didn't have the old comments in mind!
  • Johnykutty
    Johnykutty almost 12 years
    Hi @TimKrueger, I have created a repository in my MAC. And tried to push a project from another MAC in my local network. But failed.Then I tried as you said. from my mac itself but also then when I try to push it says no remotes found. How can I create a repository in my LAN and Work with my Co-developeers
  • Tim
    Tim almost 12 years
    @Johnykutty There are more then on problem possible I think. Are the folder with the repository shared in your network? Do you use gitolite or an other tool? And I think there are many more question. Create a new question on stackoverflow and explain all details to your problem.
  • Johnykutty
    Johnykutty almost 12 years
    @TimKrueger thank You for the reply :) Can you suggest a tutorial for setting up a git repository in my private network, Sorry I am a newbee to this field :(
  • Tim
    Tim almost 12 years
    @Johnykutty Only if you can German! I suggest the official documentation: git-scm.com/documentation
  • Robert
    Robert over 11 years
    Late in the day, I know, but thanks for this. Really easy to follow and worked a treat.
  • Sneha
    Sneha over 11 years
    @Tim Krueger thanks for the great tutorial but i have some trouble with the steps . When i choose to push the changes . I don't get a create option. Can you tell me how can i get RepoName/master(Create).
  • Tim
    Tim over 11 years
    @sneha Have you really considered the preconditions?
  • Sneha
    Sneha over 11 years
    @Tim Krueger yeah there was a simple mistake while creating the repo on git Hub website
  • nzs
    nzs about 11 years
    I'm getting HTTP403 when trying to push (error: The requested URL returned error: 403 while accessing github.com ... /info/refs Anybody got this error and overcome it? I'm an xcode4.2
  • picciano
    picciano about 11 years
    This method "mostly" worked for me. I had to use the HTTPS URL for the location rather than the SSL URL. Otherwise, worked like a champ. Thanks!
  • Tim
    Tim about 11 years
    @codedad I tried to reproduce your error. Is it an open repository? Can you post the url of it?
  • nzs
    nzs about 11 years
    @Tim: it was my repo to answer an SO question: github.com/codedad/ioselbowconnector.git which I added to github using Terminal. Recently I've moved to xcode4.6.1, hopefully it manages better.