When hosting a website, a decent way to manage your files is with some kind of (distributed) version control. It’s often smart to experiment and type drafts somewhere offline, on your own laptop/PC for example, before uploading them to you web server. Using distributed version controls can help you by making this process easier. If you mess anything up, you can return to a previously working state, and you can use the distributed version control’s means of syncing to upload all your changes.
If you have been following my previous posts, you’ll see I currently use Pelican and plain-text files with Markdown markup. You will also have noticed I’ve set up a FreeBSD jail on my server and installed Pelican on my laptop. But now we need to get the stuff I write on my laptop to my server. This is where
git comes in.
If you haven’t heard about
git, you should read up about it. No need to go all technical, just understanding the basics will be enough. A good guide on
git can be found at http://git-scm.com/book. If you read through Chapter 1 and some bits of Chapter 2, you will know enough to get started using
git for yourself.
Before we continue, you will need to have the following set up on your server:
- a user account (and home directory)
- OpenSSH, so you can log in remotely
If you haven’t, you should do that now. As I will not cover those here.
First, you should install
git on your machine. Once that is done, you can turn your website’s files into a git repository and commit changes to it.
I run Debian GNU/Linux Wheezy on my laptop at the moment. So installing
git on this is a breeze. Just open a terminal and type:
stefan@aether:~$ sudo apt-get install git
Adapt this to your needs. But this should work in this form on pretty much any Debian machine and its derivatives.
This is optional, but you should tell
git a little about you. If you ever plan to work with other people using
git you will have to, because knowing who committed what is pretty much always a must.
There are multiple ways to do this, the Git Book - Chapter 1.5 explains how to do this using
It’s also possible to edit the
~/.gitconfig file by hand. Just fill in the blanks in this example and save it:
[user] name = Your Name email = firstname.lastname@example.org [color] status = auto diff = auto branch = auto interactive = auto
Make it a repo
Next you are going to turn your website’s directory into a repository, so
git will track changes in it. It will not touch your files unless you command it to.
Go to the directory of your website and initialize it as a repo:
stefan@aether:~$ cd ~/proj/blog/ stefan@aether:~/proj/blog$ git init
Commit the current contents to
stefan@aether:~/proj/blog$ git add . stefan@aether:~/proj/blog$ git commit -m "My first commit."
And we’re done working locally for now.
On your server you will also need to install
git. Followed by creating a bare git repository in which to store the website and it’s history, and setting up a
git hook to checkout files from the repository to a directory you want to serve your website from.
I will perform this on the FreeBSD jail I set up in an earlier post. I have also set up a user account for myself there and enabled the SSH daemon so I can log in remotely.
Remember, you need to log in as root to install packages/ports. I used
qjail console satyr for that.
satyr /root >cd /usr/ports/devel/git satyr /usr/ports/devel/git >make config install clean
For the purpose we will be using
git here, you can safely turn off most (if not all) of the configuration options of the
git Port. This helps to keep dependencies, and with it overall installation size, down. I enabled just three options:
[*] ETCSHELLS [*] ICONV [*] NLS
OK and continue with the installation. Once completed, you can log out.
Setup as user
Now log into your server/jail as the user you set up.
Create a directory for the bare
git repo and one which will contain the actual files to point your web server at later. And create a
post-receive hook to checkout files from the bare repo to the other directory. The directory to checkout to could also be somewhere in
/usr/local/www (or any other default dir for a web server), just make sure your user has the right to write to it. Here is how I did it:
$ mkdir blog.git $ mkdir blog.sjoosten.nl $ cd blog.git/ $ git init --bare $ vi hooks/post-receive
post-receive file I put the following:
#!/bin/sh GIT_WORK_TREE=/usr/home/stefan/blog.sjoosten.nl git checkout -f
Adapt this to your needs and save it. Make sure it is executable:
$ chmod 755 hooks/post-receive
All that is left to do is point your web server at the directory you filled in as
GIT_WORK_TREE concatenated with the location of the actual .html files. In Pelican’s case you add
/output to it. I will provide an example of this in a later blog post about setting up a web server to host your blog.
Almost done! Back to your local machine…
git repository you just set up on your server as a remote repo on your local machine.
stefan@aether:~$ cd proj/blog/ stefan@aether:~/proj/blog$ git remote add blog ssh://email@example.com/~/blog.git
Substitute the name of the remote, user, domain and directory for your situation.
Now you are ready to push your local repository to the server’s bare repo. Which will in turn extract the content to the directory specified in the
post-receive hook. Go ahead with:
stefan@aether:~/proj/blog$ git push blog master
If something went wrong,
git will let you know. In case it does go wrong, make sure your directory paths are correct, and your user has the right to write to the places you want
git to write to.