Git Web Development with gitosis

Looks like I am the one with the late article this week. Hopefully it is worth the wait for some of you.

As mentioned in my article last week I’m going to go through how to setup a similar workflow to what I use here at Gaslamp Games.  Before I get ahead of myself I should probably start at the beginning for those who might stumble on this post with very little previous experience with git.  This article assumes you know your way around a Linux command line and can install packages on your system with no problem.

So what is git?

Git is a “free & open source, distributed version control system” and while that may be a quick blurb about what git is, it is exactly what it says.  Every time you clone a git repository (more on that later) you not only get the entire repository but you get every commit, a full history, and my favorite part; once cloned there is no need to have a central server or even network access.  This allows me to pull the latest changes, pack  up my laptop and head somewhere excluded when I need to really get some work done. There is quite a bit to know about exactly how git works but that is well beyond the scope of this article.  If you are curious how exactly how it all works I would suggest you get a cup of coffee and see the excellent video over at gitcasts I referenced last week.

Step One: Git Install

This is usually the easiest part of this process. For a Debian based system simply run:

apt-get install git-core

Now that we have all the components installed lets get to it.

Step Two: Gitosis Install

Gitosis is a great little tool. Normally adding an shell account to a server every time you want a user to be added is extremely time consuming and always invites the possibility for things to go wrong. With gitosis you can create a single shell account that has no password and no ability to access the rest of the system.  With this user you can create multiple virtual repositories which you can manage with a simple text file.  All the user authentication is done over SSH using keypairs to establish the user identifier.  I’ve followed the directions over at gitosis and they are solid an work; If you have any problems with the setup do not continue with this guide, things will only get messy.

Step Three: Setting Up the Workflow

What we are going to setup is the following:

  • A Prime location:  This location will be our live website. Ideally you should never directly edit a website. It is not so professional for a possible user to come by when you are working are realize you are testing/developing at that very moment. On our server this example location will be at: /home/webuser/www/ which is under the user webuser
  • A Hub location: This location is will be our ‘central’ location of sorts: From here your prime will be able to pull any development code or branch once you have worked out any bugs yourself.  This is also where you will clone from when you want to have the repository on your local machine.  This will be a repository setup through gitosis-admin under the git user.

The following is the code you need to enter to create your Prime location:

cd /home/webuser/www
git init
git add .
git commit -m "Inital import"
git remote add hub git@<servername or IP>:myproject_web.git

NOTE: This is for an already existing website.  If you currently have no files you need to add –bare after git init. (That is two dashes not one before bare)

Now to setup our hub we have to clone the gitosis-admin.git repo that was created during our installation of gitosis to establish permissions:

cd /home/webuser
git clone git@<servername or IP>:gitosis-admin.git
cd gitosis-admin

vi gitosis.conf

In this file insert the following. Adjust the names to fit your situation.

[group websiteadmin]
members = websuer
writeable = myproject_web

Save and exit out of the text editor.  We now need to commit our changes. To do this we enter:

cd /home/webuser
git commit -a -m "Added webadmin group with permissions to myproject_web
git push

Some text should fly by and your changes have been accepted.  Now gitosis has what it needs to establish permission for the repository myproject_web but it has not been created yet.  To create the repository we follow a similar process to creating our prime directory.  The only caveat is that we can’t use git init –bare because if the environment variable GIT_DIR isn’t set it will try and set it and this causes problems.

cd /home/webuser
mkdir myproject_web
cd myproject_web
touch README
git init
git add .
git remote add origin git@<servername or IP>:myproject_web.git
git push origin master:refs/heads/master

Our new repository has been created user the user git and is managed by the gitosis software. What we have to do now is get our prime and hub synced.  Currently Prime has all the webfiles but doesn’t have the README we touched and the hub only has the README file.  To merge type the following:

cd /home/webuser/www
git branch init_merge
git checkout init_merge
git pull hub master
git checkout master
git push hub master

Now everything should be synced up.  You can now add a user following the directions at the gitosis setup page and are ready to clone.  For reference to clone from the hub type:

git clone git@<servername or IP>:myproject_web.git

This will clone the entire hub where you can make edits on your local machine and ideally test them in some sort of virtual environment so you are positive they work. Once you are ready to publish you follow a two step process.

Step One: Push changes to hub from local machine

git push hub

Step Two: Pull changes to prime location.  You need to be in your prime directory to do this

cd /home/webuser/www
git pull hub master

WHEW! While it takes a bit of effort to get things going I believe this is a very effective and professional way to work with web design using git.  I want to acknowledge the joemaller article on which I adapted to use gitosis for use on a single server.  Please feel free to comment if you are having any troubles or anything isn’t clear and I would be more than happy to help you out.

Good luck!

Posted in Gaslamp, Website | Tagged ,
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *