Getting Started with Git on Mac OS X

Some Background

I’ve recently switched from using SVN to using Git as my source control management tool of choice. It took a little while to get to grips with, but it rocks!

Git is not an evolution of SVN. It is entirely different. Git is a distributed revision control system – everybody working on a project has their own full copy of the repository and its entire history.

Git does not require the presence of a network connection: most commands in Git operate locally, allowing you to review your project history, make branches and tags, merge changes, but most importantly, allowing you to commit while offline.

If you’ve used SVN you’ve probably noticed the littering of .svn folders across your entire project tree, requiring the need for an export tool to retrieve clean copies of your code, and introducing problems when you delete, rename or move files or folders in your project. Git takes a slightly more sensible approach: it creates one folder (named .git) in the root folder of your project.

Git Concepts

With Git there are 3 important things to be aware of: the Git object database, the “index”, and your working area.

The Git object database contains a bunch of stuff that you typically don’t need to worry about. Essentially it’s the repository.

Your working area is the collection of files currently sitting in your project folder. You should treat these files as if they are temporary – because they are! Things you want to keep should be committed to the repository.

The Index is sometimes referred to as the Staging Area. It’s a list of what will get stored on your next commit. Conceptually it sits between your working files and the Git object database. Files that haven’t been staged to the index will be ignored when doing a commit. This gives you great control over what gets committed when.

A branch is a cheap copy of a particular state of the Git object database. Branches are easy to create and switch between, giving you a safe way to try out ideas quickly and easily. Switching to another branch updates your working area to reflect the state of that branch – this is why you should consider your working area temporary.

Installing Git on Mac OS X

Grab and install the latest OS-X Installer from:

http://code.google.com/p/git-osx-installer/

Configuring Git

You need to tell Git who you are – it uses this information for your commits etc.

Fire up your terminal, and type:

git config --global user.name "Your Name"
git config --global user.email your@email.com

Using Git

Once Git is installed on your system, creating a Git repository is easy: open your terminal, cd into the root folder of your project, and type: git init

That’s pretty much it!

After initialising your Git repository, it will most likely be empty. In order to commit files to the repository you first need to “stage” them by adding them to the index. To add all the files in your current project to the index type:

git add .

To commit these files type:

git commit -m "your commit message"

To view the status of your repository type:

git status

To Summarise

[Download and install Git]
[cd into the root of your project]

git init
ls -la
git status
git add .
git status
git commit -m "a useful commit message"
git status

[change some files in your working area]

git status
git diff
git commit -a -m "commit message. -a adds all files to the index"
git status

[delete some files from your local working area]

git status
git add -u
git commit -m "commit message. -u adds deleted files to the index"
git status

Ignoring Files

You will probably want to ensure than certain files don’t ever get committed to the repo:

http://www.kernel.org/pub/software/scm/git/docs/gitignore.html

Extra Configuration

You can enabled colorful output:

git config --global color.diff auto
git config --global color.status auto
git config --global color.branch auto

Some Bundled Visual Tools

gitk
git gui

Git Screencasts

http://gitcasts.com/

Installing Git on Windows

http://gitcasts.com/posts/git-on-windows

Some Other Git Links

Eclipse Integration

Point your eclipse update manager to: http://www.jgit.org/update-site

More info: http://www.jgit.org/

Some Commands

Add all working files to the index:

git add .

Add deleted files to the index:

git add -u

Create and switch to a new branch:

git checkout -b newbranchname

Review last commit:

git show
git show --stat
git show --name -status
git show HEAD

Review commit history:

git log
git log tag..branch
git log -10
git log --since="May 1" --until="June 1"
git log --author=fred
git log -- some/file

Creating branches:

git branch name
git branch name commit

Switching branches:

git checkout name
git checkout -f name
Posted in Resources, Tutorials | Tagged , , , , , , | 23 Comments
  • Dan N.

    Thanks for this guide! I had been putting off the setup of a revisioning system on my mac because I couldn’t find a good tutorial with examples. Many thanks!

  • Dan N.

    Thanks for this guide! I had been putting off the setup of a revisioning system on my mac because I couldn’t find a good tutorial with examples. Many thanks!

  • http://shaun.boyblack.co.za/blog/ shaun

    No problem, hope it was helpful.

  • http://www.boyblack.co.za shaun

    No problem, hope it was helpful.

  • http://twitter.com/elness Mike Elness

    I think the various “git config -global” commands should instead be “git config –global” (two dashes instead of one).

  • http://twitter.com/elness Mike Elness

    I think the various “git config -global” commands should instead be “git config –global” (two dashes instead of one).

  • http://shaun.boyblack.co.za/blog/ shaun

    Hi Mike,

    Thanks for the heads up. Yeh, it seems WordPress converts the double dash into a single dash :(

    I’ll see if I can fix the formatting..

    Cheers,

  • http://shaun.boyblack.co.za/ shaun

    Hi Mike,

    Thanks for the heads up. Yeh, it seems WordPress converts the double dash into a single dash :(

    I’ll see if I can fix the formatting..

    Cheers,

  • Jon Toland

    I also highly recommend GitX for front end although I occasionally fall back to Git GUI and rarely terminal.

  • Steven Gentile

    great starting 'how to' – much appreciated

  • Nikos

    good post mate, using git now :)

  • Pingback: Daneomatic » Splitting Subversion into Multiple Git Repositories

  • http://www.resumewritingservice.biz/ first resume

    It really was. Thanks a lot!

  • Glen

    Really appreciate this! I spent too many frustrating hours attempting to get git working on my Mac due to other tutorials on the web, that just didn’t quite do it for me. Yours was just what I needed to get from frustration to working – Thanks!

  • http://www.facebook.com/people/Terrence-Cox/100000653643176 Terrence Cox

    Thanx! It was a big help. :-)

  • Siorlu

    Thanks a lot! very usefull! I owe you a beer :)

  • Legendaryaoustic

    Thanks! Just one thing: I think that you must write: git commit -m ‘the message’ and not use “. :)

  • http://twitter.com/casidiablo Cristian

    Thank you! Color highlighting worked like a charm!

  • ronopolis

    You forgot the most famous git training video:  http://www.youtube.com/watch?v=CDeG4S-mJts&feature=feedu  It only makes sense after you’ve tried git for a few weeks.

  • http://www.destinyislands.com/ Destiny Islands

    Yes truth here sir!

  • Derry

    Thanks for the simple GIT overview, just getting started – easier than SVN hell I was going through.

  • Pingback: A few links on Git | TechnoHippy

  • git_user

    The following command can be used for git ui color highlighting:

    git config –global color.ui auto