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

I.

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

II.

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):

#!/bin/sh
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).

III.

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

I.

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" 

II.

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

cd /local/pathtodev/www
git remote add web ssh://user@axample.com:21000/home/pathtodirectory/www.git

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. _

III.

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

git push web master

IV.

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.

Philippines 2019 Aug 19, 2019

June 2019 Jun 01, 2019

May 2019 May 01, 2019

April 2019 Apr 01, 2019

March 2019 Mar 01, 2019

Team Us Feb 26, 2019

February 2019 Feb 01, 2019

December 2018 Dec 19, 2018

July 2018 Jul 01, 2018

June 2018 Jun 01, 2018

May 2018 May 01, 2018

April 2018 Apr 01, 2018

December 2017 Dec 03, 2017

November 2017 Nov 05, 2017

October 2017 Oct 01, 2017

Bandina Week May 31, 2017

Book Review: The Plot to Kill God Apr 27, 2016

John (10 song series) Apr 27, 2016

Song for Songs Dec 31, 2015

Falling Away Aug 26, 2015

Easy README Files on Your Mac Jan 28, 2015

Word Clouds from the Gospels (and Acts) Sep 30, 2014

Lord of Your Life? Sep 21, 2014

Search the NY Times Archive Jul 24, 2014

The Farthest Peak Jul 08, 2014

Red River (When You See Me) Jun 26, 2014

Mapping shapefile polygons May 10, 2014

Using git to publish a website Sep 05, 2013

Roads We Don't Choose Aug 30, 2013

Killswitch/Mute Pedal Jul 09, 2013

Hold Out Jun 23, 2013

Muddle-headed Tyranny Jun 11, 2013

Arduino Arcade Controller May 13, 2013

A Place We Have Not Met May 04, 2013

Remember the Little Things Mar 27, 2013