Using Git to Manage a Live Site
Uploading files to production level after every change in code can be quite a hassle. Most people would use an automated deployment system instead of doing it manually. We can create an automated deployment system ourself by just using a version control like Git, which would also only take a few seconds to set up.
This post outlines the exact steps on how to do that. You should be able to understand basic terminology like pushing
or pulling
.
Install Prerequisites
Install Git on the server- and client-side.
$ yum install git-core
Head to this page if you still don’t know how to install it.
Server Setup
You’ll want to start by creating a bare repository on your server. A folder without its actual source files. The repository should be set up somewhere outside of your web root. We are going to instruct Git where to put the actual files later. Once you decide on a location for your repository, go ahead and create the bare repository:
mkdir mysite.git
cd mysite.git
git init --bare
Now we need to instruct Git where to put the files after every commit. This can be done via hooks, basically actions defined by triggers.
Head to this page to read more about hooks.
Create a file named post-receive in the .git/hooks directory of your bare repository with the following content (replace the destination path as you see fit):
#!/bin/sh
GIT_WORK_TREE=/var/www/mysite git checkout -f
Once this is done, mark it as executable:
$ chmod +x post-receive
Git is going to put the files after every push directly to the destination folder (e.g. /var/www/mysite
).
Now setup Apache Webserver, Nginx or whatever you use to serve your site from the destination folder.
Client Setup
Proceed to create the local repository and make the first commit and push by adding the remote repository we created:
$ git init
$ git add .
$ git commit -m "First commit"
$ git remote add live ssh://username@ip:port/path/to/mysite.git
$ git push live master
Now every commit and push, as in every change you make on your local repository, will immediately affect the live environment. Not even a manual pull has to be executed!