Serve Static Website in a Server using Git

This post will give a short instruction to serve your own static website (i.e. a website only contains static assets).

Local git repository

First of all, assume you have a local revision of the website, containing basic HTML files, images, JavaScript files, CSS style-sheets, etc, and the website is maintained by a private git repository.

The post receive hook

Before deploying your local revision of the website, you need to add a post-receive hook in the server side to let the website well-served by the server web engine. For example, assume you use nginx to serve the remote website, and it’s served in the server side folder /var/www/my_blog, what you need to do is, checkout the website files into the folder without the git repository information (i.e. .git folder). This can be done by the script:

rm -rf /var/www/my_blog/*
git --work-tree=/var/www/my_blog --git-dir=/srv/git/my_blog.git checkout -f

Deploy the website by git push

Finally, we can change your local revision of the website, then git commit && git push the changes. After the change pushed successfully, the updated version will be copied to the right folder in the server side (e.g. the /var/www/my_blog/ mentioned below).