9lzkww
Last Updated: February 25, 2016
·
8.039K
· kinjacono

Setting up a git daemon on Mac OS X

Git is great for distributed version control - you can share your commits with other developers without having to push to a remote origin. Very useful if you have multiple pieces of work going on in the same area of the codebase, for example. Running a git daemon is as straightforward as:

$ cd /Users/jim/git/repos
$ git daemon --export-all --base-path=.

At this point you should make sure remote login is enabled on your Mac. If you're not sure, go to Settings -> Sharing and ensure Remote Login is checked. Note the SSH command it tells you under "Remote Login: On".

Now from another machine on your network you should be able to add your Mac as a remote in git from within a repo:

$ cd /Users/charley/git/repos/teh-codez
$ git remote add jim git://[Jim's IP address]:9418/teh-codez

To check the remotes you have set up:

$ git remote -v

To pull any commits from the remote:

$ git pull jim master

If you get connection refused errors, ensure that you can SSH into the remote server and check that the git daemon is running on port 9418, the default.

Running the git daemon as a service

Create a launchd configuration plist like so, saved under /Library/LaunchDaemons:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Label</key>
        <string>Casper git daemon</string>
        <key>UserName</key>
        <string>jim</string>
        <key>OnDemand</key>
        <false/>
        <key>ProgramArguments</key>
        <array>
                <string>/usr/bin/git</string>
                <string>daemon</string>
                <string>--base-path=/Users/jim/git/repos</string>
                <string>--export-all</string>
        </array>
</dict>
</plist>

To check the configuration has worked it can be loaded using launchctl (if you have git daemon launched from the terminal, kill it first):

$ launchctl
launchd%  /Library/LaunchDaemons/jim-git-daemon.plist 

The git daemon should now be up and running, and should always be started when you boot up your Mac.