Update a website with Git

This article is basically cribbed from a Nerdess ( link) article about the same topic. I used this method when first beginning my blog, though I’m now thinking about changing to rsync or something similar. Either way, this is here for posterity’s sake, for what it’s worth.

On your computer

This is basic git setup-type stuff. If it doesn’t make sense, you’ll need to look at a git tutorial, because this isn’t one.

  1. Download and install git, if you haven’t, and create a new repository in a folder for your site:

     git init
  2. Write your site however you write it, then add and commit everything to git:

     git add *
     git commit -m "First commit"

On the server

I use Nearly Free Speech for my server, but this should work with pretty much any server with SSH access.

  1. Connect to your server with SSH, navigate to your public directory (with NFSN it’s /home/public), and run the following:

     mkdir .git
     cd .git
     git init --bare
  2. Now that the server repository has been made, you need to make sure that files pushed to the repository are published to the public folder. We’ll do that with a post-receive hook:

     vim hooks/post-receive
  3. A hook in git is just a shell script that runs on certain events, in this case, after git receives files from a push. The most trivial post-receive hook that’ll work is this:

     GIT_WORK_TREE=/home/public git checkout -f

    Since beginning this blog, however, I’ve made my script a little more complicated. The above file worked just fine though, especially if you have a lot of static files you want to serve.

  4. Make sure the post-receive hook has the right permissions:

     chmod +x hooks/post-receive

Back on your computer

  1. This step need to be done back home, as it were – you just need to add your newly-created remote to git:

     git remote add <name> ssh://<user>@<host>/home/public/.git

Obviously, replace <user> with your username on the server, and <host> with the hostname, and don’t forget to give your remote a good name – I used nfsn but you could use web, website, or fart, it doesn’t matter to me.

  1. The last thing you need to do is commit your changes to your website:

     git push -u <name> master

And everything should be where it needs to be. Good job! You’ve used git to update a website, you hacker you.