Using git and github on OS X
git is an alternative to SVN. I’m just starting out using it, so I’ll explain how to install it and use the github repository. Once I’ve got into it more deeply, I’ll go into detail on the pro’s and con’s.
This article is going to be most useful if someone has just asked you to start contributing to an existing project located on github. It covers everything you will need to connect to the project and start editing.
Install git
Firstly Download git from google code. If you are running a new Mac, you’ll want the intel version for Leopard.
Run the installer.
Set up a github account
Before you can contribute to a github project, you’ll need a github account.
Go to the plans page and signup for a free open source account.
Get added to the project
Send your account username to whoever is running the project you wish to work on, and they can add you to enable access.
Create a public key and add it to github
In order to access github you will need to create a public key on your machine and add it to the account. You can do this for each machine you use.
To create the public key run through the github instructions on providing an SSH key for OS X, or use mine:
Step 1 – find an existing public key file
Open terminal
Look for a .pub file inside your .ssh folder by typing
[code lang="bash"] cd .ssh ls [/code]
If you see a .pub file skip to step 3
Step 2 – create a new public key file
If you don’t have a .pub file, create one.
In terminal enter
[code lang="bash"] ssh-keygen [/code]
When prompted to select a name for the file, just hit enter. When asked for a password, enter a selected one and remember it – you will need this to access github.
Step 3 – copy the public key file
In terminal enter
[code lang="bash"] cat id_rsa.pub | pbcopy [/code]
Step 4 – enter the key into github
Then access your account details (make sure you are logged in) and locate the SSH Public keys heading. Select “Add another public key” and paste (ctrl+V) the key in there.
Clone the project you wish to contribute to
To start working on a project, you first need to clone it. The git clone command is used in this case to download a local copy of the project.
First you must choose a folder on your Mac to place the files. I use my local Sites folder for all code work.
Navigate to this folder from within terminal, e.g.
[code lang="bash"] cd Sites [/code]
Clone the project from github using the following command:
[code lang="bash"] git clone git@github.com:accountname/repositoryname.git [/code]
In the above example, accountname refers to the account name that the project you will work on is stored under, repositoryname is the name of the project.
A folder will be created inside your chosen folder with the name repositoryname, containing all the files.
Checkout a branch of the project to work on
To start working on the code you will need to checkout a branch of the clone.
To do this, navigate to the folder which contains the clone using Terminal.
[code lang="bash"] cd Sites/repositoryname [/code]
Checkout a branch, which you can name myBranch or anything you wish
[code lang="bash"] git checkout -b myBranch [/code]
This will then switch the files inside the folder to a branch.
Add and remove files
When you edit files, you can don’t need to anything more.
To add a new file, use the command:
[code lang="bash"] git add path/to/file [/code]
To remove a file (and this also removes it from your local area, so be careful):
[code lang="bash"] git rm path/to/file [/code]
Commit changes back to the branch when you’re done
You can commit changes as often as you like.
Navigate to the folder which contains the clone using Terminal.
[code lang="bash"] cd Sites/repositoryname [/code]
Commit changes
[code lang="bash"] git commit -a -m "message" [/code]
Rolling back
To revert back to the state of the last commit, enter the following:
[code lang="bash"] git reset --hard HEAD [/code]
Push the branch back to the team
When you have made all your local changes, you can send them up to the team hosting the project.
Navigate to the folder which contains the clone using Terminal.
[code lang="bash"] cd Sites/repositoryname [/code]
Push branch
[code lang="bash"] git push origin myBranch [/code]
Remove a branch from the server
[code lang="bash"] git push origin :myBranch [/code]
Merging changes
To bring changes down from the main path, first fetch the changes:
[code lang="bash"] git fetch origin [/code]
then merge with your branch
[code lang="bash"] git merge origin/myBranch [/code]
More info
You should find the following guides useful:



Great tutorial! Very useful. Thank you very much!
On January 25th, 2010 at 11:57 am, Importing to IntelliJ IDEA From GitHub said
[...] IDEA 9 due to the inbuilt GitHub support it provides. Prior to completing the below steps I have GitHub installed on my Mac and have setup a SSH Key (also see here). Here’s a step by step example using my [...]