29

Possible Duplicate:
Using Git with an existing Xcode project

Setting up a git repository in Xcode after a project was created. (i.e. you did not create a git repository when creating the project)

Cœur
  • 37,241
  • 25
  • 195
  • 267
Joey
  • 1,144
  • 3
  • 15
  • 29
  • very similar, the only difference is i do not include UserInterfaceState in the git repository which can get kind of annoying because it updates everyday you navigate in xcode – Joey Jun 13 '12 at 19:46
  • why not accept my answer? it works quite well for years now... – Motti Shneor Jan 07 '21 at 08:28

2 Answers2

99

Last answer works OK, but is rather lengthy and incompatible with newer Xcode versions. I will try to reiterate it better:

  1. Choose your git repository folder (directory) usually this will be the directory containing the Xcode workspace or project.
  2. In that directory, create a text file named ".gitignore", and put the following contents into it:

    UserInterfaceState.xcuserstate
    build
    *.pbxuser
    *.perspectivev3
    *.mode1v3
    *~
    *~.nib
    *~.xib 
    .DS_Store 
    xcuserdata/
    
  3. Quit Xcode (If it was open)
  4. In Terminal do the following:

    \> cd <path of the repository folder>
    \> git init
    \> git add .
    \> git commit -m "Initial commit - or whatever text you'd prefer"
    

You're done! Open the workspace/project in Xcode and examine your repository in the organizer window.

Motti Shneor
  • 2,095
  • 1
  • 18
  • 24
  • I get `git: command not found` when I try any git command. Do I need to install it from somewhere first? I've never used git before, so consider this a newbie question. – Victor Engel Dec 15 '12 at 17:08
  • 1
    I installed [git from here](http://git-scm.com/downloads), and that resolved my problem. – Victor Engel Dec 29 '12 at 03:13
  • 1
    Thanks for this. I'm just wondering, why do you add nib and xib to gitignore? – Darren Oct 09 '13 at 08:30
  • These are NOT your .nib or .xib files. Alongside a real XXX.xib you may sometimes find a XXX~.xib created as temporary backup by the editor, in case it crashes. They should be ignored by any source-control. It's a bad idea to bring them back anyway. – Motti Shneor Oct 10 '13 at 09:34
  • 1
    This was really was I was after, getting rid of keeping track of backup files, and files that tracks the user interface. It worked for me as of dec 29. 2013. This is not a duplicate post, it has too much value added compared to the other one mentioned above. – McUsr Dec 29 '13 at 02:13
  • @MottiShneor Hi. I use Cocoapods to manage my dependencies so I have a workspace. I initialized git and created the `.gitignore` file on the topmost level of the directory hierarchy. Do I have to specify absolute paths to (ex:xcuserdata) in the `.gitignore` file? – Isuru Jun 23 '14 at 06:03
  • It is a bit annoying that they don't have a command under the Source Control menu for this. – Maury Markowitz Aug 29 '15 at 15:29
  • for Victor - Mac OS X has git preinstalled - I wonder how you got not to have it. Try installing "Xcode tools" (from the app store, or from Apple developer site). I recommend on using the OS's version, and not to install something else - to keep compatibility with Xcode's git integration. Xcode itself does not implement git, but rather uses the same git commands I gave in my example. – Motti Shneor Nov 24 '15 at 10:54
  • I followed these steps, but nothing changed. My new project appeared in the old repository. If you have some suggestions then please help! Should I change "Derived Data" and "Archives" locations in Preferences? – Adam Sep 09 '16 at 04:55
  • Sorry I don't understand your issue. My answer is for pre-existing project. if you have an Xcode project that HAS NO repository to start with. Also, what do you mean by "in the old repository" ? If an Xcode project is within a git repository - obviously it is part of it. There is absolutely no relation to the derived-data or preferences. Last - when you create a new project in Xcode - it asks you whether to create a repo for it. Say "YES" and you're done! – Motti Shneor Sep 13 '16 at 16:14
19
  1. Quit Xcode (not sure if this is necessary but I do it just in case)
  2. Run Terminal
  3. Get into the project folder directory
  4. find .
  5. Find the file that says "UserInterfaceState.xcuserstate" and copy the entire filename up to the ./
  6. echo "paste the UserInterfaceState.xcuserstate file here" >.gitignore
  7. cat .gitignore
  8. git init
  9. git add .
  10. git commit -m "You can type a comment here like now under source control"

You now have a repository and your project is under source control

Joey
  • 1,144
  • 3
  • 15
  • 29
  • 2
    I'm not sure this is a good way because 1) It overwrites the existing .gitignore file contents that needs more important files to be ignored. 2) It ignores specific files by name, instead of a reasonable single rule (like *.xcuserstate) for instance. In my suggestion for .girignore file above, I added all my user (non-shared) scheme files. There must be an easy way to do the same for user state files of the interface editor. – Motti Shneor Dec 30 '13 at 07:26