Git push for website publication
This tutorial walks you through the process of setting up git so you can easily push to staging and production remotes.
You should have a local development copy of your website (located anywhere) and staging and production websites in the same directory on your remove server; e.g. two directories named staging.website.com
and website.com
.
You can certainly locate them anywhere you want (just change the path to the remotes in step three) but I'm not planning for that in this guide.
You probably already have your local git repo initialized, but if not, run git init
. You don't need to do anything else (yet).
Now we'll move over to your server. You need to make your staging and production directories (if you haven't already), initialize git repos, and do a bit of configuration and prep.
If you have your staging/production directories in place already, cd
into them, make sure they're empty, and skip to the second line below.
$ mkdir staging.website.com && cd $_
$ git init
$ git config receive.denyCurrentBranch ignore
$ touch .git/hooks/post-receive
$ chmod +x .git/hooks/post-receive
The above sample is for a staging folder. Repeat this process for your production folder (e.g. website.com
).
post-receive
hookIn both staging and production, add the below to .git/hooks/post-receive
:
#!/bin/sh
GIT_WORK_TREE=../ git checkout -f
echo 'Post-receive hook executed!'
Finally, we need to add the appropriate remotes on dev and staging.
Important: Remotes chain directly upward; e.g. dev pushes to staging, and staging pushes to production. However, dev can not push to production. Production is the end of the chain; it doesn't push anywhere.
Note: If you're in the mood, you may want to add production as a read-only remote to dev, so you can sync production back to dev on a whim. But that's only if you have a good reason to. I wouldn't recommend it as a default.
$ git remote add staging ssh://user@hostname/path/to/staging.website.com/
$ git remote add production ../website.com/
Note: I use relative paths from staging to production, but you can use absolute paths as well. In fact, relative paths may break on older versions of Git, so use with caution. I still think it's more convenient this way, and allows you to move things around without lots of reconfiguration.
Now, it's just a matter of git push staging master
if you're on dev, or git push production master
if you're in staging.
Tada!