78

I have JUST created a new github repo on the website. Keep in mind that I am a noob but can do basic things in terminal. Anyway, so I just created the repo on my account on github.com and didn't do a single thing to it yet.

So all I want to do is take my Xcode project folder and upload it to my new github repo. How do I do this via terminal?

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
SimplyKiwi
  • 12,376
  • 22
  • 105
  • 191

7 Answers7

86

The best way really is to follow the instructions GitHub gives you:

First, go the correct directory

cd <directory of your Xcode project>

It sound like you've already got a local Git project from Xcode. If that's true, skip this code block.

git init .
git add .
git commit -s
<type in a commit message>

Last, push into your repository. The following is copied directly from my own github account, after I created a new "test" repo. Change the "dhalperi/test" part.

Push an existing repository from the command line

git remote add origin git@github.com:dhalperi/test.git
git push -u origin master
Dan Halperin
  • 2,207
  • 1
  • 18
  • 25
  • Thanks, one issue I am running into is that I am getting this: Permission denied (publickey). fatal: The remote end hung up unexpectedly What am I doing wrong? – SimplyKiwi Oct 02 '12 at 21:30
  • 1
    @iBradApps: It's trying to use SSH to connect to GitHub. Looks like your public key is wrong. Have you set up keys? If not, use the HTTPS version of the repository address: `` – Dan Halperin Oct 02 '12 at 21:51
  • 8
    Adding all files xcode generates may be a bad idea since it will probably cause conflicts. When I last did xcode development i added these three lines to my .gitignore (not sure if the list is exhaustive): MyProj.xcodeproj/project.xcworkspace MyProj.xcodeproj/xcshareddata MyProj.xcodeproj/xcuserdata – Adam Bergmark Oct 03 '12 at 05:42
66

I was just going to leave a comment above but I will also add a summarized answer. My full answer is here.

This isn't via terminal as the question requests, but you can consider this an alternate method. It is well integrated with Xcode now, though, and not difficult to do.

Create Git repository when making a new project

enter image description here

See the answer I linked to above if you already have a project without a Git repository.

Make a Commit

enter image description here

After making a change in your project, select Commit... from the Source Control menu. You will add a short commit message.

Create a new GitHub repository

enter image description here

On the GitHub website make a new repository. Call it whatever you want, but don't add a README or .gitignore or license yet. After creating the new repository copy the link to it.

Add a Remote in Xcode

in Xcode go to Source Control > your branch name > Configure. Then click the Remote tab > "+" button > Add remote. Fill in your repository name and address.

enter image description here

Push your project to GitHub

Go to Source Control > Push. You will have to enter your GitHub user name and password the first time.

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

If you didn't follow this explanation, please read this and this for more detail.

Further Study

This is an excellent video that I recently watched that helped me understand Git and its integration with GitHub much better.

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • what if i have made several commits already? i am on build 7.0 of version 1.0 of my app and there are several commits i have made. i want them all to reflect on my github private repo. will what u have done achieve this or only show the last commit? – AceN Sep 30 '15 at 07:06
  • @AceNeerav, If you make several commits and then push them to GitHub, all of the commits are saved and the changes can be browsed from GitHub. See [this link](http://stackoverflow.com/questions/17888643/view-the-change-history-of-a-file-using-github-com) for how to view them. – Suragch Sep 30 '15 at 12:05
  • @Suragch I do not have any branches. I just want to push whole project to github repository. Im getting stuck at this point : Source Control > your branch name > Configure. There is no branch so how do i add a remote in this case? – Mamta Mar 19 '18 at 04:42
  • @Mamta, The branch should be `master`, but if that doesn't work, try asking a new question and include more details. I don't recall it asking for the branch when I did it so there may be some differences now. – Suragch Mar 19 '18 at 04:57
  • @Suragch please check new question posted https://stackoverflow.com/questions/49356282/xcode-9-2-unable-to-configure-git-source-control-for-master-branch – Mamta Mar 19 '18 at 05:22
14

An easier way to do it in Xcode. You can even push only one current branch to GitHub or the whole project.

(for Xcode 11)

  1. Just select a needed branch/(or top folder) in "Source control navigator" and right-click on it. Then select "Create remote". Or "Editor -> Create remote". enter image description here

  2. Then use your account at GitHub to create the repository. If using your GitHub account is not proposed maybe you still not add your GitHub account in Xcode -> Preferences -> Accounts enter image description here

  3. After everything is set up. Click "Create".
  4. To push your project/branch to GitHub repository click "Source Control -> Push".

After that check Github. All should be there.

Oleh H
  • 846
  • 1
  • 10
  • 18
7

Get the Github Mac Application, you don't really need a terminal unless you want to do more fancy advanced stuff ;-)

Simply go to your project page on GitHub, then click on "Clone in Mac", this should automatically download and install it for you. You have to do this at least once to install the app and setup some required permissions.

After that, you'll see the GitHub Application in your Dock, simply open it, then click on that small "+" sign at the bottom, there you'll see "Create New Repository" and "Add New Local Repository".

You can either add the repository that XCode already created for you or download one from GitHub, then open it in XCode.

Martin Baulig
  • 3,010
  • 1
  • 17
  • 22
4

if you already have a git repo on your Mac and using Xcode 9,

for xcode 9, just go to the source control tab in the navigation inspector in Xcode. right click on the master git and select create "Your project name" remote on GitHub.

give your GitHub credentials and give a name to git repo and Xcode will create and push your code for you. enter image description here

Vimal
  • 187
  • 6
2

a non terminal solution is to install SourceTree. Its a GUI thats super easy to use compared to the terminal. But its highly recommended to learn git commands properly.

mihai
  • 4,184
  • 3
  • 26
  • 27
  • I would suggest that Source Tree shows an enormous amount of information that can be overwhelming to git beginners that don't have a firm grasp on the basics. – HenryRootTwo Jun 27 '15 at 00:57
  • @StuartGrimshaw, i completely agree but I feel the tutorials for ST beginners is easier to follow which would be a good introduction to git. – mihai Jun 29 '15 at 17:40
0

Recent (2023) answer (I'm on Xcode 14, but suspect this has been available for a while): Assuming you enabled source control when you created your Xcode project (enabled by default),

  • Go to your source control navigator, select "Repositories", right/secondary click on "Remotes" under your project name, and add a new remote.

  • Xcode will walk you through logging into GitHub (or other remote git repositories), including name selection. No need for any extra software, and no need to create a repo on remote side first.

Xcode screenshot

P.T.
  • 13
  • 4