Tuesday, February 5, 2013

Setting up Git on a Server


I've been testing out Git a bit more recently. The main feature that interests me is the way Git branches. It's light-weight and is managed pretty well by the Git software. for more information, see Chapter 3 of the Git Book.

My plan is to use Git for synchronizing changes I make in my local development for my web server with the live version. I also want Git to manage a development server path which will switch to the branch I last pushed to for testing purposes. Luckily, both of these are fairly simple operations.

Server Setup

Here's the command to install git:

sudo apt-get install git-core

This installs the git-core package as well as the git package. There are a variety of other git packages you could install, though for right now I'm not using any of them.

The first thing to do is find a place for our "blessed" repository. This is a shared bare repository on our server which serves as the master repo. I chose a bare repository for a few reasons:

  1. Supposedly with recent versions of Git (1.7.0 or newer), in order to push changes to a repository it needs to be bare. I haven't tried this out myself, but here's a source I found with the relevant information.
  2. I don't want my server location to hold a working copy. This is in a secure location so I can control access. I'll be cloning this repo for the live/development working copies that the webserver can use.
  3. A shared repo can be pushed to.

I'm storing all of my git repos inside the /opt/git/ repos. Since I do my web development in Eclipse, I also made a sub-folder for all of my Eclipse projects.

As far as I know Git isn't setup well to checkout sub folders from a repository in the same manner SVN is. For this reason I decided that each Eclipse project should be its own Git repository.

# quick-tip: the -p option creates all necessary folders to get to the end leaf
mkdir -p /opt/git/eclipse/project1.git
cd /opt/git/eclipse/project1.git
git init --shared --bare

Next step is to create a hook which will update my server each time I push to the main repo. I'm using a post-update hook for this. The hooks should be placed in the /opt/git/eclipse/project1/hooks/ folder. The file should be executable.

For more information on git hooks, see 7.3 Customizing Git - Git Hooks.

# by: helloworld922

# Get the branch which was pushed to
branch=$(git rev-parse --symbolic --abbrev-ref $1)

# Necessary so we can pull changes in another repo
# see: http://serverfault.com/questions/107608/git-post-receive-hook-with-git-pull-failed-to-find-a-valid-git-directory
unset $(git rev-parse --local-env-vars)

if [ $branch = "master" ];
 # update master branch as the live version
 cd "/home/helloworld922/www/public/project1"
 git pull

# switch our developer's repo to the branch and update it
cd "/home/helloworld922/www/public/project1_test"
git checkout -t "origin/$branch" -B "$branch"
git pull

The last step is to create the initial clones of the repositories.

# this is where my live version is located
mkdir -p /home/helloworld922/www/public/project1
cd /home/helloworld922/www/public/project1
git clone "/opt/git/eclipse/project1.git" .

# this is where my development test version is located
mkdir -p /home/helloworld922/www/public/project1_test
cd /home/helloworld922/www/public/project1_test
git clone "/opt/git/eclipse/project1.git" .

Git by itself doesn't provide any authentication mechanism. Instead, it relies on the access protocol to authenticate users. For example, using the SSH protocol I can use the standard filesystem permissions to manage access to the repository.

# On the server
# make a group which owns the master repo
sudo groupadd git_project1
# add the users you want to the group
sudo usermod -a -G git_project1 helloworld922
sudo usermod -a -G git_project1 root
# set the repo's ownership and permissions
cd /opt/git/eclipse/project1.git
sudo chown -R root:git_project1 .
# allow anyone in the group to read/write
sudo chmod -R ug+rw .
# everyone else can only read
sudo chmod -R o+r-w .

Client Development Setup Over SSH

The url to the repository is: username@host:full_path. For example, my username is helloworld922, my virtual server has a static IP at, and the full path to my Git repo is /opt/git/eclipse/project1.git. This makes my clone url helloworld922@

That's all for now. I now have access to virtually the awesome branching features offered by Git without compromising the way I develop and deploy my content.

No comments :

Post a Comment