1

When I start a new project with GitHub I always struggle with the same issue. When I create a GitHub project it's already prepopulated with some files (.gitignore, LICENSE, README.md) and hence with an initial commit. I explicitly choose to add these items so I don't have to care about writing them myself.

On the other side, when I start a new Xcode project it works in a very similar way: Xcode creates an initial commit with some files. So when I'm trying to pull my GitHub repo I always have to deal with Git refusing to merge unrelated histories problem.

Is there a correct workflow for this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ivan Yurchenko
  • 3,762
  • 1
  • 21
  • 35
  • You can create initial repo at github without files, as far as i remember. When you create repo, you have option to create "empty" repo and push your local one to github. – Oo.oO May 09 '17 at 04:07
  • @mko I know but as I mentioned I want to add these files so I don't have to write/manually copy them myself. – Ivan Yurchenko May 09 '17 at 04:08
  • Does `--allow-unrelated-histories` not solve your issue? – AnilRedshift May 09 '17 at 04:10
  • @AnilRedshift Yep it does, but it just looks a bit improper to me to always do it this way. I was wondering if there's some "correct" approach for this case. If there aren't any, I'll just continue doing it as I do. – Ivan Yurchenko May 09 '17 at 04:15
  • The flag exactly describes what you want to do: You want to have two initializers work together. Your basic options are 1) nuke the .git folder and re-add everything yourself as a fresh commit 2) Manually take the files from inititializer A and copy it to initializer B 3) Use --allow-unrelated-histories to have git do it for you – AnilRedshift May 09 '17 at 04:20
  • @AnilRedshift Huh, so there's no other way? :( I see, thanks for the explanations. – Ivan Yurchenko May 09 '17 at 04:35

2 Answers2

2

Here is the easy way to do this, assuming that you are using a recent Xcode, e.g. 11.2...

  1. create new repository in GitHub, checking .gitignore file and README.md file options
  2. copy repository URL from browser's address field
  3. create new project in Xcode, checking local git repository option
  4. right click Remotes in project's source control navigator to add remote, using URL copied earlier, suffixed with .git
  5. select Fetch and Refresh Status from Source Control menu
  6. select Pull from Source Control menu, from origin/master remote (.gitignore and README.md files are now in your local repository... if in Finder, use Command Shift . to toggle hidden files display)
  7. select Push from Source Control menu, to origin/master remote (Xcode project files are now in your GitHub repository)

Voila! You have set up a new project in Xcode and GitHub in less than a minute.

jmdecombe
  • 782
  • 6
  • 16
0

Just came across the same issue. It actually works on Xcode 11, if you follow these steps:

  1. Create the repository on GitHub including initial branches, License, Readme.md etc.
  2. At the Welcome screen of Xcode choose "Clone an existing project"
  3. Clone your repository into the desired directory (usually a subdirectory of XCodeWorkspaces)
  4. Close Xcode and reopen to get back to the Welcome Screen (that's the trick)
  5. This time choose "Create a new Xcode project"
    • Place the project into the cloned directory. Xcode automatically picks up, that this directory is already under Git control.
  6. You'll see that the initial files are marked with A and M in the Xcode project navigator
  7. In the menu "Source Control" choose "Commit". You should see all files created by Xcode. Make sure to activate on "Push to remote:" and choose the right branch. Press Commit

If nobody committed or changed on GitHub between step 1 and step 7, it'll work. Check on GitHub.

jboi
  • 11,324
  • 4
  • 36
  • 43