Skip to content

Instantly share code, notes, and snippets.

@tekkub
Created June 3, 2010 01:44
Show Gist options
  • Save tekkub/423308 to your computer and use it in GitHub Desktop.
Save tekkub/423308 to your computer and use it in GitHub Desktop.

There are precompiled versions available for Windows, Linux (via apt-get/yum/etc) and Mac OS X. In addition to the precompiled versions, you can also install git via MacPorts. However, if you want the most recent version, or to install it in a non-standard location, compiling it from source is actually very easy (on Mac OS X at least).

You need to have Xcode installed, which provides a version of GCC. The most recent version can be obtained for free from the Apple Developer Connection. If you don’t have a fast internet connection or have some other reason not to download Xcode via Apple’s site, then you can always install Xcode from the Mac OS X DVD (from the folder labeled ‘Optional Installs’).

You can download the latest stable version of git from git’s website, in the form of a .tar.gz or .tar.bz2. The built in Mac OS X archiving utility can extract this for you, or you can extract it yourself:

Alcarin:~ steven$ cd ~/Downloads
Alcarin:Downloads steven$ tar -xjf git-1.6.1.3.tar.bz2

And then cd to the directory:

Alcarin:Downloads steven$ cd git-1.6.1.3

Alternatively, if you have a version of git already installed, you can get the source from the git repository (but be warned, it’s less tested):

Alcarin:~ steven$ git clone git://git.kernel.org/pub/scm/git/git.git
Alcarin:~ steven$ cd git

It’s recommended that when you install git, you put it in its own folder, i.e. /opt/git/ to keep things tidy. It’s also far easier to remove or install new versions (as it’s a single folder you can delete easily).

Git comes with the appropriate files to allow you to use automake to generate a configure script and Makefile, but since a Makefile is already provided with git, it’s generally unnecessary and more clunky to use automake (it’s available, it’s just discouraged).

If you really want to use automake, do this:

Alcarin:git-1.6.1.3 steven$ autoreconf
Alcarin:git-1.6.1.3 steven$ ./configure --prefix=/opt/git
configure: CHECKS for programs
checking for cc... cc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
[...]
Alcarin:git-1.6.1.3 steven$ make

Otherwise, you can simply do:

Alcarin:git-1.6.1.3 steven$ make prefix=/opt/git

On a Core 2 Duo 2.33GHz, building this way takes a minute and ten seconds. If you want it to build a bit faster and you have a multi-core machine, you can add the -j# flag to make (where # is a number, i.e. “make -j2”) to have it run more than one job at once. A good choice is the number of CPUs in your system plus one, but this guideline isn’t always perfect.

Next, you need to install the compiled binaries:

Alcarin:git-1.6.1.3 steven$ sudo make install prefix=/opt/git

You should note that the git man pages are not installed at this point. They’re extremely helpful, so I recommend you install them. To do so, you first need to install ‘asciidoc’ and ‘xmlto’, which can be installed through MacPorts. If you haven’t already done so, install MacPorts and then run this:

Alcarin:git-1.6.1.3 steven$ sudo port install asciidoc xmlto

This will take a while, so go have a cup of coffee or some nachos.

Once those are installed, build the man pages in the git source directory:

Alcarin:git-1.6.1.3 steven$ make man prefix=/opt/git

And install them:

Alcarin:git-1.6.1.3 steven$ sudo make install-man prefix=/opt/git

Great! Now you’ve got git installed, along with its handy-dandy documentation. But before you celebrate with some beer and pizza (if it really means that much to you), you should make it easier to invoke git. With the way you currently have git installed, it’s not in your “PATH”, which means you have to type ‘/opt/git/bin/git’ every time you want to invoke git. This is far more cumbersome than necessary. The best way to fix this is to add /opt/git/bin to your PATH, so all you have to type is just ‘git’. I have a heavily customized .bash_profile file in my home directory which I use for this purpose:

for a in local $(ls /opt/ | grep -v local | grep -v gentoo); do
	FULLPATH=/opt/$a
	if [ -x $FULLPATH ]; then
		if [ -x $FULLPATH/bin ]; then
			export PATH="$FULLPATH/bin:$PATH"
		fi
		if [ -x $FULLPATH/sbin ]; then
			export PATH="$FULLPATH/sbin:$PATH"
		fi
		if [ -x $FULLPATH/share/aclocal ]; then
			export ACLOCAL_FLAGS="-I $FULLPATH/share/aclocal $ACLOCAL_FLAGS"
		fi
		if [ -x $FULLPATH/man ]; then
			export MANPATH="$FULLPATH/man:$MANPATH"
		fi
		if [ -x $FULLPATH/share/man ]; then
			export MANPATH="$FULLPATH/share/man:$MANPATH"
		fi
		if [ -x $FULLPATH/lib/pkgconfig ]; then
			export PKG_CONFIG_PATH="$FULLPATH/lib/pkgconfig/:$PKG_CONFIG_PATH"
		fi
	fi
done

When you open a new Terminal window, you should just be able to type ‘git’ and it will behave as follows:

Alcarin:~ steven$ git
usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [-p|--paginate|--no-pager] [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE] [--help] COMMAND [ARGS]

The most commonly used git commands are:
   add        Add file contents to the index
   bisect     Find the change that introduced a bug by binary search
   branch     List, create, or delete branches
   checkout   Checkout a branch or paths to the working tree
   clone      Clone a repository into a new directory
   commit     Record changes to the repository
   diff       Show changes between commits, commit and working tree, etc
   fetch      Download objects and refs from another repository
   grep       Print lines matching a pattern
   init       Create an empty git repository or reinitialize an existing one
   log        Show commit logs
   merge      Join two or more development histories together
   mv         Move or rename a file, a directory, or a symlink
   pull       Fetch from and merge with another repository or a local branch
   push       Update remote refs along with associated objects
   rebase     Forward-port local commits to the updated upstream head
   reset      Reset current HEAD to the specified state
   rm         Remove files from the working tree and from the index
   show       Show various types of objects
   status     Show the working tree status
   tag        Create, list, delete or verify a tag object signed with GPG

See 'git help COMMAND' for more information on a specific command.
Alcarin:~ steven$

Congratulations! You now have a working git install on Mac OS X.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment