Publishing Using GIT

Git, for those unaware, is a version control system. It tracks changes that you make to files and lets you review history, revert back to older versions, and do all sorts of useful stuff. It comes into its own when you work with groups of developers - you can each work on different parts of a project, and then very cleanly merge your changes together into an authoritative master version of the codebase.

Even if, like me, you’re the only one working on your code it’s still a great tool. So far I haven’t had to revert back to an old version of my code, but it’s nice to know I could if I wanted to. What I primarily use it for however is (in my opinion) way more useful than its stated purpose.

Publishing Websites Using Git

I have a webserver within my home network. It’s open to the world and I could host my little teeny-tiny web empire on it probably quite nicely. But commercial webhosts offer much better uptime than I might get from using my home internet connection, and certainly much better download speeds for end users like you. So I don’t. I have a VPS that hosts the “production” versions of my web work, and I use my home server as a “development” environment.

What I use git for is publishing from the development to the production server. If you have a similar setup using a local xAMP stack for development then you can probably replicate my setup quite nicely to your advantage. It makes publishing to the web a one-step process, and it uses SSH so the publishing is done over a secure connection.

The prerequisites:

Strictly speaking that last point isn’t a requirement, but this setup only really comes into its own if you can test code locally and then only publish it once you’re happy with the results.

The Server

We’ll take care of the server side of things first. Create a directory outside your webserver root that will hold the git meta-data. On my server the web root is at /home/account/domain/. I think this is atypical, so your set-up may be different.

mkdir /home/account/domain.git
cd /home/account/domain.git
git init --bare

Next we need to tell git where to actually put the files:

git config core.worktree /home/account/domain
git config core.bare false
git config receive.denycurrentbranch ignore

Finally, we need to tell git what to do for us when new files are pushed to the server’s git repository. Create a file called hooks/post-receive

vi hooks/post-receive

In the file:

git checkout -f

This file needs to be executable.

chmod +x hooks/post-receive

The Local Machine

Navigate to the directory where the website code lives.

git init
git add .
git commit -m 'Initial Commit'

Next, link the repository on the webserver:

git remote add origin ssh://[email protected]/home/account/domain.git

Finally, “push” all the content from the local machine to the server:

git push origin master

Done! Depending on the size of your codebase and speed of your connection it may take a few moments the first time, but since git is smart enough to only push content that’s changed, it will be quicker next time.

From now on, whenever you’ve made some changes on the development machine that are ready to be published to production, run:

git add .
git commit -a -m ‘Change Description’
git push

I put these three lines into a little bash script, and I can publish using a little web control panel I made using PHP (relying on PHP’s exec function). Also in my control panel I have the ability to push and pull databases from the server thanks to this page and the comments on it. Simple!

Comments & Discussion