Home News WebDev Links About Garden Notes LAMP Stack Using Git Using SSH/SFTP Linux Commands
Git logo

Git is a free and open source distributed version control system

Built to be small and fast, but able to handle huge repositories (think the entire Linux core), Git is easy to use for beginners and code-mavens alike.

Using Git to Update a Live Website

Say you have a website on a remote server and you do your coding on your local desktop. Here is an easy way to set up git as a version control system that can send your local master repo to the webserver where it automatically updates the webroot.

Nothing fancy is needed -- just your local computer and your webserver. Things like github or staging servers, or fancy gui programs can be added later, but first let's get the basics working.

Here's how:

Set up Git on Local


cd /my/project/directory
git init    ## sets up local git directory
git add .   ## stages all files
git status  ## shows list of files awaiting commit
git commit -m "my commit message"   ## adds files to local master repo
      

Set up password-less SSH

By using pre-shared public-key/private-key SSH access to your server, Git can automatically log on to your server and update the website files. The following steps assume you have already configured public and private keys and shared them. If you have not, there are a couple of good articles in the box at right.

Even if you already have public/private keys, do one check before going on. Can you log in to your server simply by typing "ssh yourusername@yourwebserver.com" or do you have to give ssh the path to your keyfile as well (something like "ssh -i /path/to/my/keyfile myusername@mywebserver.com)? If the latter, use the ssh-add command to add the path to your key into the SSH authentication agent. Check the ssh-add article in the box for details.

Configure the Remote Server Repository

Now switch over to your webserver.

Set up a bare repository


cd ~
mkdir mywebsite.git
git init --bare --shared
        

Add a Post-Receive Hook - this tells git what to do when it gets an update.


cd ~/mywebsite.git/hooks
touch post-receive

## add the following 2 lines to the file post-receive:
#!/bin/sh
GIT_WORK_TREE=/path/to/webroot/of/mywebsite git checkout -f

## and make it executable
chmod +x ~/mywebsite.git/hooks/post-receive
      

GIT_WORK_TREE tells git the location of webroot that is to be updated.

Do the Initial Push to the Webserver

In your project directory on Local:


git remote add live ssh://user@mywebserver.com/home/user/mywebsite.git
git push live +master:refs/heads/master
      

The first line tells git to remember a remote repository called "live" that is located on your webserver. The second line tells git to push the local repo, master, to the remote repo, live.

Ongoing Workflow

You now can follow this simple workflow going forward:

1. Edit your code files in your favorite editor.

2. Process in git


# Stage
git add *.html

# Commit
git commit -m "my commit message"

# Push to server
git push live   ## note simplified version