Managing a FTP-Website with Git

In my case I owned webspace and wanted to manage it with Git. The common way to checkout a git-repo on update or just periodically wouldn't work, because I am not allowed to run code. But it turns out that all you need is Git on a local machine or on another server, and ftp access.

return home

1. Initializing a Git Repo

This tutorial builds on this tutorial on toroid.org

First thing is to create a Git Repository. In case of this website I initialized it on a server. You could do the same on your local machine. (You'd need to change one command later on).
$ mkdir example.git
$ cd example.git/
$ git init --bare
After that is done you need to make a folder to 'mirror' your files to. This folder is later being used to upload files from.
$ cd
$ mkdir example.org
after doing that, you want to add a hook to your git repo.

2. Setting up Git Hooks

This hook has to checkout the newest version after accepting it (post-update) and then needs to upload it to your destination webspace via ftp. The best ftp client to upload a whole directory I found was ncftp.
$ apt-get install ncftp
$ nano example.git/hooks/post-update
Edit the file so it says:
#!/bin/sh
GIT_WORK_TREE=/home/pi/tibyte_nitrado.net/ git checkout -f
ncftpput -u [FTPUSER] -p [FTPPASSWORD] -R [WEBSITE] / [PATH OF "example.org/"]/*
#e.g. ncftpput -u admin -p 12345 -R www.example.org / /home/admin/example.org/*
Save and exit (CTRL+O, ENTER, CTRL+X).

3. Pushing changes

You first have to set up your local git directory, for that all you need to do is clone the remote repo.
$ git clone user@domain:example.git
After that you can push your website to the repo and it will automatically be uploaded to your webspace.
$ git add -A
$ git commit -m "init"
$ git push origin master
That's it.

PS: every time you push something to the git repo your only your diff's get sent (good), but the repo-server has to send all files to the webspace, because he can't know what the webspace looks like. This means the publishing process, i.e. sending all files over ftp can take a while (bad).

diagram of how files are sent

4. Advanced: Workflow

You don't want to overflow your Git Repo with changes, even more so when even push means that you have to reupload the whole page (I am looking for a way around that. "ncftpput -A ..." might work). To test local files in a nice way you can set up a series of things.

* might not always be a good idea, because of absolute file paths.

I want to talk about the latter.

5. Atom and php-server

I am not involved in the developement of the Atom editor nor am i connected to the plugin php-server, it's just what I use and what I am happy with.
  1. Install Atom
  2. Install php-server
  3. Run server on your repo root directory

return home