Website version control with Git

Some notes on using git to manage development and production versions of a website on a Linux server, based on Using Git to manage a web site.  There seem to be several web pages with similar ideas out there: I don’t know who wrote it down first.  And also with reference to Version Control with Git by Jon Loeliger.

I’ve adapted those ideas for the way I like to do things:

  • I SSH in to the server, and do the editing there, using vim.
  • I have separate domains for development and production versions of my sites.  For the purposes of these notes, they’re called dev.example.org and www.example.org.  So the development version is also an active real-world website: my nginx configuration makes it only visible to me.
  • The document roots are /var/www/website and /var/www/website-dev respectively.
  • The ‘bare’ production git repository can be anywhere on the server.  I’ll put it at /var/www/website.git.  It’s a git convention to use the .git extension for bare repositories.

The steps for setting it up are as follows.  I’ll leave the setting of suitable permissions and use of sudo as an exercise for the reader.

  1. Put some web pages in /var/www/website-dev.
  2. mkdir /var/www/website
    cd /var/www/website-dev
    git init
    git add <all the appropriate files and directories>
    
  3. Create a .gitignore file, and add everything that shouldn’t be transferred to production, such as configuration files that specify the development database and debug settings. For a WordPress site, that probably includes most of the WordPress stuff.
  4. git commit -a -m "a message"
    mkdir /var/www/website.git
    cd /var/www/website.git
    git --bare init
  5. Create /var/www/website.git/hooks/post-receive containing:

     

    #!/bin/bash
    GIT_WORK_TREE=/var/www/website git checkout -f
  6. In the following, I’ve used ‘live’ as an alias for the production environment; you could use ‘prod’ or whatever you fancy.
  7. chmod +x /var/www/website.git/hoots/post-receive
    cd /var/www/website-dev
    git remote add live file:///var/www/website.git
    git push live +master:refs/heads/master
    git push --set-upstream live master
    git push live
  8. And, as if by magic, the files from the master branch of /var/www/website-dev are now in /var/www/website.
  9. Then whenever you’ve got new code ready to into production, all that’s required is:

     

    git push live

2 thoughts on “Website version control with Git

  1. Luke

    Hello,

    Very good – got a development and a live site working with this. However, if by any chance the live site gets a new image added – my development site doesnt have this image. If i run

    <code>git pull live master</code>

    It comes back as “Already up-to-date” 

    I am assuming this is becase I push to the repo, then the live site gets from the repo. However I would like the live site to push to the repo in this instance so that I can pull from the repo to the development site.  

    I hope that made sense, any ideas how i could do that?

    Thanks

    Reply
    1. Chris

      Hi Luke

      Thanks for your comments.

      It’s designed as a one-way system, pushing from development to production.

      I don’t know enough about git to work out if it’s possible to make it go the other way.

      Perhaps you could use something other than git to copy images from production to development.

      Cheers,

      Chris

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *