David King

full stack developer

All Blog Articles

Prev « The best programmers CV I could think of Next » Deploying with Git(HUB)


Deploying with Git > handy hints

30 Jan 2012

With regards to deploying your site with git, there's some potentially confusing posts out there; a quick google for "git deployment" will turn up a few (seemingly) outdated posts (I'm using git version 1.7.4.1 at the time of writing), ie:

If you take a peek at the toroid post you'll read:

The one-line summary: push into a remote repository that has a detached work tree, and a post-receive hook that runs "git checkout -f".

Which I now understand to mean:

When you push to a remote repo a script will run that updates another directory (that's the "detached work tree" bit)

Perfect! I've already got my servers up and running so I don't want to start moving directories about the place, so for me this is what I did to pretty seamlessly add git deployments

SSH into the remote machine...

# Move to the /var/www/ folder
cd /var/www/
# Make a new folder for our bare git repo
mkdir html.git
cd html.git
# Intialise a bare git repo
git init --bare
# Create our post-receive hook file (this simply writes 2 lines to the file)
echo -ne '#!/bin/sh\nGIT_WORK_TREE=/var/www/html git checkout -f' > hooks/post-receive
# Make our post-receive file executable
chmod +x hooks/post-receive

On your local machine you can now push to the remote repository and your working directory will be updated:

# Add the remote repo
git remote add www ssh://username@IPorURL/var/www/html.git
# Now push
git push www master

And you're done. That was simple!

Quick word of warning

My local repo also runs on a local server, for me I must be careful of my /uploads/ folder as scripts can write to that directory, I don't want those files tracked by git so I use a .gitignore file to make sure that no hiccups occur in that department :-)


Prev « The best programmers CV I could think of Next » Deploying with Git(HUB)