Deploying with Git > handy hints
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 188.8.131.52 at the time of writing), ie:
I had a few problems with this one when I got to the push stage, apparently this occurs with newer versions of git.
while written for specific deployment platforms I ran into the same problems as the ryanfolrence post
now we’re talking! Not only is this kept up-to-date but works a treat :-)
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
- Create a git repo in /var/www/html.git/
- Have that repo update my working directory /var/www/html/
- err… that’s it!
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 :-)