Git Setup - A Server And Web Based Workflow In About 10 Minutes

Early on in my career I tracked my code changes using a text editor then pushed those changes over FTP, I knew there had to be a better way. Eventually, I was introduced to concurrent version system (CVS). While this did automate the tracking process and brought some since of sanity to team coding efforts, it still didn’t quite feel right. SVN seemed a popular choice but I saw no real value in switching over, then along came Git. Almost the very minute I started playing with Git I knew I had a winner. This is everything version control should be. I knew instantly I should be using Git however, the question did remain; how?

It’s not just enough to say, “Yes, this is a superior version control system, we must use it.”. It needs to be able to work with you, without getting in the way. As web developer I need to be able to push from my dev machines into testing and production environments and it needs to be seamless. The process should be something like change code, push to repository, pull from repository and new code is live. Once we have determined Git can accommodate the desired workflow (and it can) we’ll need to get started with the set up. If your new to Git I would suggest first reading the documentation. If your eager to get started they have a quick and dirty tutorial to Git you going. I know Git can feel a little over whelming at first, but don’t worry; we will map out a workflow, install Git and have the Git server (the repository) up and running in about 10 minutes.

The tutorial

I’m assuming you’ve just got your web server up and running and now, your ready to deal with version control. We’ll assume you’ll be building a web site for example.com. This tutorial will walk you through the steps I used to set up a git server and web based workflow on Debian 5 (Lenny) Linux. First open a shell. Via the shell, login to your server, install git , and create a git user (for this tutorial we’ll just call the user git).
Replace xxx.xxx.xxx.xxx with your server’s IP address.

ssh root@xxx.xxx.xxx.xxx
apt-get install git-core
sudo adduser git
exit

Logout of your root account and login as your git user. Create a directory for your project (example.git), switch to your new directory and initialize the directory as a git repository. Then logout as the git user.

ssh git@xxx.xxx.xxx.xxx
mkdir example.git && cd example.git
git --bare init
exit

On your development machine create a path to the local copy of your website. Next, initialize the sites root directory as a git directory. Then, you’ll want to create an empty README file to give us something to push. At this point, we are ready to stage and commit our README file. Once committed, we’ll add a remote, this will allow us to push and pull by calling origin. Finally, we will push to origin.

cd /var/www/vhosts/example.com/httpdocs
git init
touch README
git add README
git commit -m 'first commit'
git remote add origin git@xxx.xxx.xxx.xxx:example.git
git push origin master

Now we have our git server running and our development repository. We now need to pull our code into our production site. To do this we will initialize out production site the same way we did our local site. Login as root (or whatever user has the appropriate privileges). Switch one directory up from root and clone the git repo, giving it an alias, since I’ve become accustomed to PLESK based servers I’ll use their path convention and clone the repository as httpdocs. Then log out as the privileged user.

ssh root@xxx.xxx.xxx.xxx
cd /var/www/vhosts/example.com
git clone /home/git/example.git httpdocs
exit

Now we can push and pull using just our branch names since master is the default that’s we’ll use. Refer to Git’s documentation for more information on branches.

Now, we’re ready to test our workflow. Navigate to your web directory and open the README file. Once open, type “testing” or some like text. Stage the changes, commit and push to origin.

cd /var/www/vhosts/example.com/httpdocs
vim README
git add README
git commit -m 'Testing GIT install'
git push origin master

Once again login to your server as root (or a properly privledged user). Navigate to your web directory and pull from origin.

ssh root@xxx.xxx.xxx.xxx
cd /var/www/vhosts/example.com/httpdocs
git pull origin master

Now, by navigating to your websites read me file (www.example.com/README), you’ll now be able to see your changes on the live site.