Tutorial: How to use Git to deploy and update a website Pt. 1


This tutorial outline the steps I took to push to my remote Git Server, and update my website with one command simultaneously.

“git push web”

The Problem

I routinely push the changes I made on my website to a remote server that host my website and stores the git repository. It's somewhat redundant, because after I push the updates I made using git I use an FTP client to upload the same files to the same server o.0. Yes! There must be a way to automate this process, and kill two birds with one stone.

The local repository

It doesn’t really matter how the local repository is set up, but for the sake of argument, let’s suppose you’re starting one from scratch.

mkdir website && cd website
echo ''Hello, world!'' > index.html
git add index.html
git commit -q -m "The humble beginnings of my web site."

The remote repository

I assume that the web site hosted on a server to which you have ssh access, and that things are set up so that you can ssh to it without having to type a password.

On the server, we create a new repository to mirror the local one.

cd /home/project/
mkdir website.git && cd website.git
git init --bare --shared
$ Initialized empty Git repository in /home/project/website.git/

Then we define (and enable) a post-receive hook that checks out the latest tree into the web server’s DocumentRoot.

mkdir /var/www/www.example.org
cat > hooks/post-receive
git checkout -f
chmod +x hooks/post-receive

Back on the workstation, we define a name for the remote mirror, and then mirror to it, creating a new “master” branch there.

git remote add web ssh://[email protected]/home/project/website.git
git push web master

Now we can push to our remote repository and the webserver DocumentRoot with one command.

git push web

Easy Right?