PHP Developer / Blog

April
10th, 2008

How to get started hosting your git repository using GitHub and OSX

Digg this article · Save to del.icio.us · Stumble it!

Hosting remote git repositories using GitHub is completely painless. Assuming 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 then you’ll want to do that now. If you don’t already have a public key then 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 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 an paste it in and save it.

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 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.

Creative ideas on using GitHub? Let me know in the comments.

One Response to “How to get started hosting your git repository using GitHub and OSX”

  1. Psousa Says:

    great tutorial! It helped me alot.

    although I can’t seem to pull from github into my local copy,

    git pull
    You asked me to pull without telling me which branch you
    want to merge with, and ‘branch.master.merge’ in
    your configuration file does not tell me either.

    do I always need to specify the full path to my master repository?

Leave a Reply

Captcha
Enter the letters you see above.


About this site:
This is my (Jaisen Mathai) personal site for potential employers who want to see my resume or portfolio. My ideal job would be to work as a PHP developer on a large scale consumer website. My experience is in using PHP, MySQL, Ajax and JSON. I really enjoy creative brainstorming...taking a problem apart and narrowing 100 solutions down to the best one.

Thanks for stopping by. Be sure to drop me a line.