118

I am trying to figure out how to use git in my project workflow, and I have an existing Xcode project that I want to put into the repository. I think I have the repository set up correctly under organizer, but the Source Control menu is grayed out.
Apparently, it's easy to do if you start a new project, but how do I import an existing project with snapshots and everything?

I'm using Xcode 4 and git 1.7.4

Also, if there are any good walkthroughs on git configuration and best practices, that would be nice. I'm a little late to the game, so anything that can get me up to speed would be cool.

Cœur
  • 37,241
  • 25
  • 195
  • 267
AndyD273
  • 7,177
  • 12
  • 54
  • 92

7 Answers7

181

GitHub has some very good git tutorials at help.github.com.

to do the initial setup of a repository, open up a terminal window, and CD to the project directory. once there, type

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

Restart Xcode. The repository should now be set up, and you will be able to manage it in xcode 4.

Vatsal Manot
  • 17,695
  • 9
  • 44
  • 80
MCannon
  • 4,011
  • 2
  • 16
  • 12
  • 5
    Where is the bit about adding a .gitignore file to avoid adding all build directories and other crap as tracked files ??? – Fraggle Oct 12 '12 at 12:56
  • 8
    This method would add .xcuserstate and other metadata to the repo, which should not be version-controlled. I'd suggest that you first create .gitignore in your project folder and list those metadata file types before you run "git add .". – kakyo Jun 19 '13 at 22:43
  • Did not work for me at all. Xcode never recognises it as a repo. – Jonny Jul 18 '15 at 17:00
  • 1
    I'm using Xcode 11. I was able to add a local git repository to an existing project simply by doing Source Control, Commit and then doing my first commit. That created the .git folder, etc. (No need for the `git init`, `git add .` and `git commit -m`.) – Jeff Jul 22 '20 at 06:30
  • In Xcode 14 you can add git to an existing project by selecting: Source Control > New Git Repository... > Create – P. Stern Nov 13 '22 at 02:00
113

Xcode 7 (and 8)

If you were starting a new project you would just check Create Git repository during the setup. (Then skip down to the Commit part below.)

enter image description here

But it you are working with an existing project, go to Xcode > Preferences... > Source Control and check the Enable Source Control box.

enter image description here

Then in the main Source Control menu choose Create Working Copy....

enter image description here

(If you get a "Please tell me who you are" error the see this question/answer or one of the other linked questions there.)

When that has finished, make any change to one of your Xcode project files. Then go back to the Source Control menu and choose Commit.

enter image description here

And write a commit message and click the Commit button. (If the commit button is disabled, then make any minor change to your project and try again.)

enter image description here

In Github sign in and create a new repository.

enter image description here

Call it whatever you want, but don't add a README or .gitignore or license yet. You can add those things later. Doing so now will make the syncing more difficult.

enter image description here

Copy the link to your repository.

enter image description here

Go to Source Control > your branch name > Configure.

enter image description here

Click the Remotes tab > "+" button > Add remote....

enter image description here

Enter the github repository name and paste in the address.

enter image description here

After adding the remote, click Push in the Source Control menu. Enter your github user name and password. That's it. You project should be copied to github now.

(I had some trouble getting my username and password accepted at first. If that happens to you go to Xcode > Preferences... > Accounts > your new repository. Enter your user name and password there and then try the Push again.)

enter image description here

You can add a README and other files, but if you do it from the web, you will have to do Source Control > Pull in Xcode before you con commit other changes.

Now any time you make changes in Xcode, all you have to do is Commit and Push.

I learned this method mostly from here.

See also

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • this is the easiest solution by far. can u tell me what it means when u add a repo in xcode>preferences>accounts>add repo ? – AceN Sep 30 '15 at 07:35
  • @AceNeerav, This allows you to store your credentials for another repo that you have so that you don't have to enter them every time. See [this documentation](https://developer.apple.com/library/ios/recipes/xcode_help-accounts_preferences/articles/add_repository.html) for more details. – Suragch Sep 30 '15 at 12:25
  • Great solution. TIP: Bitbucket is a free alternative to github for small projects. You pay once the project gets big. – steve-o Nov 08 '16 at 17:17
  • 1
    @steve-o, [GitLab](https://about.gitlab.com/) is also a good alternative. It allows private repositories for free. I was able to configure Xcode to use it similarly to my answer above. – Suragch Nov 09 '16 at 00:12
  • thanks by following url answer i m able to do it myself thanks a lot once again – Dilip Tiwari Jul 27 '18 at 05:27
  • 1
    This has changed quite a bit in recent years. For example, the default branch is now main and the UI for Xcode 12 looks quite a bit different. – Jeff Muir May 03 '21 at 05:55
  • 1
    Is there any update for xcode 13? – Maya Jan 22 '23 at 19:22
44

I just went through the process of doing this for several projects, and ran into some issues. Here was my process, mostly pieced together from the other answers on this page/site. Order is very important here

  1. Open terminal and cd into the directory containing the .xcodeproj file
  2. git init
  3. In finder enter the newly created .git folder (in the same directory as the .xcodeproj file). Find info/exclude, open and add these lines, .DS_Store and xcuserdata/
  4. Not sure if this is needed, but I used terminal to cp info/exclude over to a .gitignore file
  5. git add .
  6. git commit -m "Initial commit"
  7. Now to get XCode to actually see the repo. In the project organizer, you first have to delete the project from the Projects tab. Then simply re-open the project to get it back. This will detect the GIT repository just created
  8. In the repositories tab of the organizer I then branch off the master branch/switch into whatever version I'm working on.

Hopefully this helps someone without being too redundant. What now seems simple was definitely a challenge for someone new to GIT with existing projects

nuclearnova
  • 785
  • 7
  • 10
10

Check out my post on this topic Setting up a git repository in XCode for a pre-existing project. The above is correct, but it will include UserInterfaceState in your changes as you commit and this could be annoying because this file updates everytime you do anything in xcode, even if it is as simple and navigating through files or folders in your project.

Community
  • 1
  • 1
Joey
  • 1,144
  • 3
  • 15
  • 29
1

if your XCODE 7 is not connecting with existing GIT repository. Try following

xcrun git config --global user.email your@email.com
xcrun git config --global user.name "your name"
Ram G.
  • 3,045
  • 2
  • 25
  • 31
1

Newer versions of Xcode simplify adding git to an existing Xcode project. Open your project in Xcode and choose Source Code > New Git Repositories. A sheet opens.

enter image description here

Select the checkbox next to the project and click the Create button to add git to the project.

Swift Dev Journal
  • 19,282
  • 4
  • 56
  • 66
0

Note:Specially for Xcode 11

CD to project directory using terminal

git init

git add .

git commit -m "Initial commit"

After this restart the Xcode and choose Source Control Navigator and then select the master and right click then choose option "Create Remote"

Vikram Sinha
  • 581
  • 1
  • 10
  • 25