Git on MacOSX

March 23rd, 2008 by

I've been playing with Git lately and wanted to share some instructions and gotchas. I've installed Git on 10.5 and 10.3 (which needs a few extras installed) but haven't tried them on 10.4 yet, let me know how it goes. From my experience, version control is way underutilized (at least in non-computer engineering) and would reduce the amount of duplicated effort, improve the ability to collaborate, and keep detailed history of important text files (like say your thesis if you're using LaTeX, which you should be). Other version control systems I've used are subversion and perforce, but I like git because it's super fast, it's pretty easy to manage remote repositories over ssh, and it's hot right now.

Initial Setup

As usual, you need to have the Developer tools installed.

Edit  ~/.bash_login (for just you) or /etc/profile (for all users on your computer)to add /usr/local/bin + sbin to your $PATH variable, e.g. add the following line:

export PATH="/usr/local/bin:/usr/local/sbin:$PATH"

Build and Install Git

curl -O http://kernel.org/pub/software/scm/git/git-1.5.4.4.tar.gz
tar -xzvf git-1.5.4.4.tar.gz
cd git-1.5.4.4
./configure --prefix=/usr/local
make all
sudo make install
cd ..

If you get a compile error about po/de.msg make[1]: *** [po/de.msg] Error 127 then you should just be able to run

export NO_MSGFMT=1
make all

assuming that you only need to install the English interface.

If you get an error like referring to expat.h then you will need to build expat using the following commands

curl -O http://surfnet.dl.sourceforge.net/sourceforge/expat/expat-2.0.1.tar.gz
tar xzvf expat-2.0.1.tar.gz
cd expat-2.0.1
./configure
make
make check
sudo make install
cd ..
source /etc/profile

To make sure that git is installed and working, trying running git from the command line.

Setting up a remote repository

In my case, I was interested in setting up our lab server (OSX 10.3) to properly host git repositories. First, install git on the server. Next, add the following to  /etc/profile, which is a handy script to make repository creation easy. The script is great and is from here.

newgit()
{
if [ -z $1 ]; then
echo "usage: $FUNCNAME project-name.git"
else
gitdir="/Library/WebServer/Documents/git/$1"
mkdir $gitdir
pushd $gitdir
git --bare init
git --bare update-server-info
chmod a+x hooks/post-update
touch git-daemon-export-ok
popd
fi
}

After creating the folder /Library/WebServer/Documents/git you can just run newgit repository.git to make an accessible repository.

Here's an important step: make sure that /etc/profile properly loads the /usr/local/bin directory (see above). When you push/pull/clone data from your server, things will not go smoothly and will get errors like git-receive-pack: command not found or git-upload-pack: command not found.

Installing gitweb

Git includes a handy .cgi program for viewing your repositories through a browser. It's quick to install assuming you have apache setup with mod_perl installed and it's setup to serve .cgi files. Checkout the gitweb directory in the git source folder after you built git earlier (you didn't delete it yet, right?)

At this point you can use git normally on your remote host. Here are some hastily written examples:

Creating a repository from a directory of existing files

cd PROJECT
git init
git add .
git commit -m "first commit"

Creating a new repository

mkdir PROJECT
cd PROJECT
git init
(create files, write code)
git add .
git commit -m "first commit"

Putting your code on your server

ssh USERNAME@YOURSERVER
newgit PROJECT.git
git push USERNAME@YOURSERVER:/Library/WebServer/Documents/git/PROJECT.git master

Pulling code from your server

git clone USERNAME@YOURSERVER:/Library/WebServer/Documents/git/PROJECT.git

Here are a few additional references to help get started (1, 2, 3, 4, and 5).

6 Responses to “Git on MacOSX”

  1. Gabriel Boucher Says:

    <cite> Many installations of sshd do not invoke your shell as the login shell when you directly run programs; what this means is that if your login shell is bash, only .bashrc is read and not .bash_profile. As a workaround, make sure .bashrc sets up $PATH so that you can run git-receive-pack program.</cite>

  2. Gabriel Boucher Says:

    I’ve got to install git on 10.4, the only thing is that you need to add /usr/local/bin and /usr/local/sbin to a .bashrc instead of .basr_profile or /etc/profile because Many installations of sshd do not invoke your shell as the login shell when you directly run programs; what this means is that if your login shell is bash, only .bashrc is read and not .bash_profile. As a workaround, make sure .bashrc sets up $PATH so that you can run git-receive-pack program.

  3. joey Says:

    Great, glad that you got it working.

  4. rich Says:

    Thanks so much. I’ve been battling the ‘expat.h is missing’ error for over a week now. After installing expat on OS X 10.3, Git is good-to-go.
    Thanks again!

  5. Club Penguin Cheats Says:

    Many installations of sshd do not invoke your shell as the login shell when you directly run programs; what this means is that if your login shell is bash, only .bashrc is read and not .bash_profile.

  6. John Pitcairn Says:

    Installed git 1.6.6.2 with no issues on a PPC running 10.4.11

Leave a Reply


fetishroom