or:

How to Use Revision Control When Your Site Can Make Changes to it’s Own Files


I’m assuming that you have setup git on a shared server, and resolved any git-upload and git-receive errors.


There are two ways to making changes to the website’s files:

  1. Web side changes: The site is updated or file attachments are uploaded.

  2. Offline changes: You make file changes (eg. theme changes or plugin adjustments) and then upload them to your web space.

Because manual integration and merging is a “disaster that you just haven’t had yet”, how is it possible to keep both sets of changes easily combined? Revision control; using git’s merging tools and distributed repository structure to keep everthing running smoothly.

Basically I’m using one bare repository as the master copy (hub repository) and automatically pull from and push to the live website folder (prime repository), while allowing remote access with manual push/pulls to make offline code adjustments (in working clones) that are pushed to the live directory automatically. All the while using git to flag up the commit problems for manual merging, so the live site never gets borked by a outdated overwrite.

git Managed, Self-Updating Website


If your browser doesn’t like vector images, and won’t show the image above, click here for a “backwards compatible” version.


The hub repository can be pushed to or pulled from by any working clone. The hub’s post-update hook gets the prime repository to pull whenever hub is updated by a working clone. This propagates any changes to the master branch into the live site, but will raise an error that needs to be manually resolved if there are uncommitted web-side changes, or the working directory is out of date.

Hub post-update hook:

`#!/bin/bash

echo “** Pull hub’s changes into prime **

cd /path/to/live/website/folder/ || exit unset GIT_DIR git pull origin master `

The working clones are both offline copies of the live site and all of it’s history, and local work areas where changes can be made and pushed to the live site (prime repository) through the hub.

The prime repository is a special case of a working clone; this repository only needs a master branch, and when any changes are commited, the post-commit hook automatically pushes the commits to the hub repository.

Prime post-commit hook: `#!/bin/bash

echo “** Push prime changes to hub **

git push origin `

A cron job on the web server makes regular, automatic commits on the web-side prime repository to record any web-side changes. If no changes have been made, nothing happens.

Script called every 12 minutes to make prime repository auto-commits: `#!/bin/bash

cd /path/to/live/website/folder/ git add . git commit -a -m “AUTOCOMMIT” git push origin master `

The .htaccess file needs additions to close off access to the .git directory and .gitignore file as they are not needed for browsing on the site.

.htacess additions: # Close access to .git/ and .gitignore RewriteRule \.git/ - [F,L] RewriteRule \.gitignore$ - [F,L]


Sources:

Many thanks to joemaller.com - A web-focused git workflow for the hooks. If you need help on the process of setting up the git repository, this site has it.