Matthew Henderson

college station, tx

Using git to publish a website

Sep 05, 2013

Using git as part of your workflow to maintain or publish a website is easier than it may at first look. Having experimented with Jekyll for site development, I was looking for an easy way to use git to update only those files which had been modified/added/removed each time. The process is actually quite simple using git — it is a natural command-line companion if you are already using Jekyll for website publishing.

I don’t claim the way shown in this tutorial is the absolute best way to handle the process, but I thought it might be helpful for someone looking for an easy way to use git for your website’s version control, and to push any changes remotely when desired with one simple command in the terminal.

Note: in the instructions below, you will see me specifying the cd command multiple times where it is not necessarily needed when going step-by-step; those lines are merely there to assist you in confirming where these steps take place on the remote system.

Note: this tutorial is assuming a *nix environment (in my case OS X and Ubuntu), with git installed on both the local and remote machines.

On the Remote Web Server


On the remote webserver, create a new directory, and initialize a bare repository; the standard is to use the .git extension for the folder name (ie. /home/pathtodirectory/www.git):

mkdir /home/pathtodirectory/www.git 
cd /home/pathtodirectory/www.git
git init --bare


On the remote web server, inside the git repository directory, create the following file in the hooks folder (it will be executed when changes are pushed to the remote host):

cd /home/pathtodirectory/www.git
sudo nano hooks/post-receive 

Edit that file (“sudo nano hooks/post-receive”; or vi if you prefer) to include the following lines, altering file locations where applicable (the path in example line 2 should be to the root folder for the website you are maintaining):

GIT_WORK_TREE=/home/pathtowww/public_html git checkout -f

When you initiate a push to the remote server, this hook file will be automatically executed so that the changes pushed from the development repository on your local machine to the remote git directory (/home/pathtodirectory/www.git) will be reflected in the remote web folder (/home/pathtowww/public_html).


On the remote server, since the post-receive file needs to be executable, you need to set the permissions:

cd /home/pathtodirectory/www.git 
sudo chmod +x hooks/post-receive

On the Local Machine


On the local machine, if you haven’t already, initialize the git repository in the development website directory:

cd /local/pathtodev/www
git init
git add -A
git commit -m "initial setup" 


On the local machine, set the remote repository that you will be pushing into:

cd /local/pathtodev/www
git remote add web ssh://

NOTE: Instead of “web” in the command above, you are free to use whatever name you wish. For instance, “production” or “remote.” It is simply the name designated for this particular remote repository.

NOTE: In this example, a non-standard SSH port (21000) is specified. If you are using the standard port, you can simply change to 22, or drop that part of the path altogether.

_NOTE: This setup assumes that you are using public key authentication against your remote server. _


On the local machine, push the local master branch to the web server:

git push web master


Thereafter, whenever local changes are made and committed, you will not need to specify you want to use the “master” branch; you only need to run the following to push the changes remotely:

git push web

That’s all there is to it. You are now set up to use git to push any changes remotely that you have made in your development environment.