Skip to main content

Notebook entry

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

Lovell Felix 2 min read

Archive note: the tools and versions have moved on. I have kept this entry because the debugging path and the underlying constraint may still be useful.

This walks through how I push changes to my remote Git server and update my live website with a single command.

The Problem

I routinely push the changes I make on my website to a remote server that hosts the site and stores the Git repository. It's redundant work: after pushing with Git, I turn around and re-upload the same files over FTP. There has to be a way to automate this and kill two birds with one stone.

The local repository

It doesn't 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

This assumes the website is hosted on a server you can reach over SSH, and that you can SSH in without typing a password each time.

On the server, 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 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
#!/bin/sh
GIT_WORK_TREE=/var/www/www.example.org
git checkout -f
chmod +x hooks/post-receive

Back on the workstation, define a name for the remote mirror, then push to it, which creates a new master branch there:

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

From here, one command pushes to the remote repository and updates the webserver's DocumentRoot at the same time:

git push web

About the author

Lovell Felix

Infrastructure and reliability engineer working on Linux platforms, configuration delivery, and deployment safety at fleet scale.

@lovellfelix

Continue through the notebook