Home | Edit | New

Guides: Setting up a remote repository using GitHub and OSX feed

Originally posted on jaisenmathai.com.

Hosting remote git repositories using GitHub is completely painless, assuming that you’ve already signed up for an account at GitHub and that you have Git installed on your computer. For OSX, you can use macports to easily get Git’s command line tools installed.

Create your public keys

If you haven’t already given GitHub your public key, you’ll want to do that now. If you don’t already have a public key, let’s do that first. Fire up a terminal and do the following:
user# cd ~/.ssh
user# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/username/.ssh/id_rsa): # press enter here
Enter passphrase (empty for no passphrase): # type in your passphrase here
Enter same passphrase again: # type your passphrase again
Your identification has been saved in /Users/username/.ssh/id_rsa.
Your public key has been saved in /Users/username/.ssh/id_rsa.pub.
The key fingerprint is:
50:43:77:c6:97:af:61:82:dc:ea:9b:6b:67:d4:1b:61 user@host
Now that you’ve generated your public key, you should see it when you do an ls. Once this is verified, you can copy the contents to your clipboard:
user# ls
known_hosts
id_rsa
id_rsa.pub
user# cat id_rsa.pub | pbcopy
Now that your public key is on your OSX clipboard head on over to http://github.com/account, paste it in, and save it. Note that git expects to use the key named ‘id_rsa’ – if you generate a key with a different name, you’re likely to get ssh errors.

Make a repository on GitHub

Next, you’ll want to create a repository on GitHub. Go to http://github.com/repositories/new and fill out the form and make note of the project name as we’ll be using that as the name of your git repository. Feel free to select either public or private.

Specify GitHub as a remote repository

Let’s move on to specifying GitHub as a remote repository on your local workstation/laptop. Head back to your terminal and do the following.
user# cd ~/repos/my_repo
user# vi .git/config
You should see a core section and will want to add a remote section. The name can be anything, but I recommend using something like “github” to make it easier to remember what you’re doing down the road. Let’s add the following to the bottom of the config file.
[remote "github"]
url = git@github.com:git_username/projectname.git
fetch =  +refs/heads/*:refs/remotes/origin/*
push = refs/heads/master:refs/heads/master
Your config file should now look something like this.
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "github"]
url = git@github.com:git_username/projectname.git
fetch =  +refs/heads/*:refs/remotes/origin/*
push = refs/heads/master:refs/heads/master
You can also do this by typing this into your terminal.
user# git remote add github git@github.com:git_username/projectname.git

Push your code to GitHub

Both essentially do the same thing, but once you’ve defined a remote git repository, you can begin pushing your code to it. Let’s test it out and see if we can push our git repository to GitHub.
user# git push github master
Enter passphrase for key '/Users/username/.ssh/id_rsa': # enter your passphrase here
updating 'refs/heads/master'
from 000000000000000000000000000000
to   871b8e6c61bc6fca6ee974bc836d70
Also local refs/remotes/origin/master
Generating pack...
Counting objects: 18
Done counting 1019 objects.
Deltifying 1019 objects...
100% (1019/1019) done
Writing 1019 objects...
100% (1019/1019) done
Total 1019 (delta 236), reused 0 (delta 0)
refs/heads/master: 000000000000000000000000000000 > 871b8e6c61bc6fca6ee974bc836d70
That’s it! Now go back to your browser and verify that all your files are showing up. Go back to http://github.com and you should see “Your Repositories” to the right-hand side with your project directly underneath it. Click on your project and you should see your files.

Comment on the original blog post.

Last edited by jk, 6 months ago
Versions: