How to Create Git Repos for Existing Xcode Projects

UPDATE: Apparently just initializing an empty repository in the project directory is enough; Xcode autodetects it for you. It be nice if something to that effect was in the documentation. Thanks to Jason for that (details here). Open the project directory in a terminal and type the following:

git init
git add .
git commit -m "Initial commit"

I got Xcode 4 - pretty impressive. I've been waiting to get this stable release since I was at the demos at WWDC last Summer. I was particularly excited about the new Git integration.

New Xcode projects will let you keep local git source control. Using an existing project with this local source control is ... difficult. I tried for a while before almost giving up. Documentation around the "Add Working Copy" is almost non-existent. This is how you do it.

  1. Move your existing project folder to your desktop.
  2. Make a new, empty project in Xcode with the same name as your project. Make sure the box for using a .git repo is selected.
  3. Close Xcode.
  4. Copy all files from the project folder on your desktop to the new project folder Xcode created.
  5. Double-click the project file.
  6. You'll now need to add all the files to the source control since (they'll all have question marks beside them, shown right).

I tried adding initializing an empty git repo and importing into Xcode with no luck. If anyone has a better solution, please let me know!

March 16th, 2011 Posted in programming

15 Responses to “How to Create Git Repos for Existing Xcode Projects”

  1. Paddy Says:

    1. Close the project (possibly xcode, cannot confirm)
    2. Open terminal
    3. Navigate to project directory
    4. $ git init
    5. $ git commit -a -m "Initial commit" (Possibly not necessary)
    6. Open project in Xcode and enjoy git goodness

  2. Ash Furrow Says:

    But how do you get the git repo into Xcode?

  3. Paddy Says:

    Xcode picks it up automatically. Jason says that he doesn't (usually) even need to reopen the project.

  4. Ash Furrow Says:

    Oh, cool! Wish that was stated in the docs :)

  5. jn Says:

    If you use the command line version that Paddy suggests, you miss out on all of the baked-in .gitignore stuff that Xcode does for a new project, don't you?

  6. Ash Furrow Says:

    Jason Brennan has also recommended a good .gitignore file available here that works well. You probably lose the default one set up by Xcode; you could create an empty project and copy their settings over to a global file.

  7. MRT128 Says:

    Adding existing project to repository

    Basicly:
    cd /Users/myUserName/Projects/Project-folder
    git init
    git add .
    git commit -m "Initial commit"

  8. objectthink Says:

    This probably should have read:

    I think what is also needed is:

    $ git init
    $ git commit -a -m "init commit"
    $ git add .
    $ git commit -a -m "initial add"

    Open xcode

  9. Vramin Says:

    This is a very helpful post, thank you.

    If you're switching from an SVN repository (which I was), the trick is to go into your project folder and delete or rename the .svn directory. For me it seemed that Xcode would find the .svn folder first (or preferred it, even though the backing repository was decommissioned), but once it was gone Xcode would happily start using the git repository.

  10. Ash Furrow Says:

    Great tip! I'd always encourage someone to switch to git from SVN :)

  11. in Xcode 4 how to associate a project to the local Git repository I set up? - Programmers Goodies Says:

    [...] From “How to Create Git Repos for Existing Xcode Projects“: [...]

  12. Anonymous Says:

    You also must delete the .svn directories from all of the subdirectories in the project, including the application bundles like (.xcodeproj, .xcdatamodelId, etc.). To track these down you can run this in your project directory: find . | grep .svn

  13. kaolin fire Says:

    My favorite way to purge svn ~

    find . -type d -name svn -exec rm -rf \{\} \;

  14. kaolin fire Says:

    er, make that -name .svn ~

    find . -type d -name .svn -exec rm -rf \{\} \;

  15. Joleen A. Says:

    This at least allows me to commit manually through the terminal, but I can't seem to use the commit function in xcode Source Control > Commit

    I get:

    The operation could not be performed because the file is not under version control.