Every programmer will one day realize that Git is easily the best available source code control on the planet. I was a fervent follower of SVN so far and even wrote SVN is the best source code control. The only reason that daunted me to use Git is to use the command line interface. But, if you master the command line, Git is all yours to command. Moreover, with tighter integration of XCode 4 and Git, I think it’s a great idea to learn about Git and migrate to Git from SVN.
In this article, I’ll take you through the bare minimum steps to setup git on your computer and start XCoding.
Before we delve into the detailed steps, a couple of words about Git.
Git, unlike SVN is a distributed version control system. which means, when you commit a repository, you actually don’t commit to the central repo. In Git, every user has his own local repo and he at his own will can “push” changes to the central repo.
Once you get this concept of distributed source code control, Git is a cakewalk. Believe me.
Now, let’s get started.
Step 1: Install Git.
Yeah, a no brainer step, pick up the installer from here
Step 2: Generate your keys
Git uses a private/public key authentication to verify YOU before you access the remote git repository.
ssh-keygen -t rsa -C ""
Just keep on pressing enter for any question it asks till your public key gets generated
Step 3: Copy keys to clipboard
But, why is this a separate step? Because you can do it easily through command line.
cat ~/.ssh/id_rsa.pub | pbcopy
Type this to “copy” the public key to clipboard (Equivalent of Cmd + C)
Step 4: Add this public key to your remote repository
Update your public key into your remote Git Repository. All Git Repositories like github, sourcerepo, beanstalk provide a interface to add your public key to your user profile.
That’s it! Now you can start using Git. See it’s that simple.
Now we shall start using Git to commit your changes and push it to the central repository.
Step 5: Create your first Git local repository
To get started, create a directory and cd into that directory
mkdir GitFolder
cd GitFolder
Step 6: Initialize your local repository
Initialize a git repo in that new directory by typing in the following command.
git init
This command initializes the empty directory you created with a git repository. Unlike SVN, git repositories are not stored in some different location. The git repo is stored within the folder you created inside a hidden folder called “.git”
Step 7: Clone a remote repository
git clone "" .
Now clone your remote URL to your current directory (.)
(Be sure to type the last “.”, it’s not full stop)
That’s it.
Your GitRepo is now on your local drive.
Step 8: Making your first commit
Now, let’s make a change and commit this to your local repo. For this, edit any file in the project and come back to Terminal.
Type
git diff
You should see the changes you made on the console. To commit these changes, type
git commit -a -m "Your commit comment"
Step 9: Pushing your changes to remote repository
Now, remember that, Git is a distributed version control system, so, these changes aren’t yet available to other users/contributors of the same remote repository. You must “push” these changes back to the remote repository. For doing this,
you have to configure a remote location using the following command.
git remote add "remote location name" ""
Your remote location starts with ssh:// and looks like this
ssh://github.com/foo/bar/MyGreatiPhoneApp.git
Your “remote location name” can be anything and most tutorials like those from github tend to use “origin”
Step 10:”Pushing” your local commits
Now that you have configured a remote location,
git push "your remote location name" master
or in short,
git push origin master
Now your code has been successfully committed to the central repo.
Step 11: “Checking out” a remote repository
“Checking out” from a git repository is called as “pull”. The command for that is very similar
git pull origin master
Note that cloning a git repo is to copy the entire repository where as pulling just updates the latest incremental changes. Unfortunately (or fortunately) there is no equivalent of clone in SVN. Just remember that you are doing this step because Git is a distributed revision control system.
Step 12: Setting up a default remote location
You can setup this location permanently as your default remote location by typing in this command
git config branch.master.remote "your remote location name"
From now on, you can just type git push and git pull to commit and retrieve changes to and from the repository.
Step 13: Adding exclusions
Every project always contains some auto generated “build” folders that need to be excluded from being committed. To do this, you use a “.gitignore” file. Create a .gitignore file and type the relative directory addresses one per line and Git automatically ignores them. My .gitignore file usually looks like this
build/
.gitignore
fbconnect/src/build/*
MyiPhoneApp.xcodeproj/mugunthkumar*
The first line is to exclude the build directory, the second line is to exclude this .gitignore file and the third line is to exclude the fbconnect (or any third party library)’s build folder and the last line is to exclude your personal XCode editor settings from being committed into the repository.
So, that’s it for setting up Git for your great iPhone App.
Git has even more powerful tools to branch and merge locally and pushing/ignoring a certain branch. But I will leave that for another post.
Happy Gitting.
–
Mugunth
No related posts.

2 Responses to “Tutorial: Setting up Git on your Mac”