Git is an awesome tool to control your production and staging builds. The basic setup uses a split tree GIT remote on your production server. You can also use this to trigger builds across many servers if you have them. More on that later.


The simple single server setup. Developer has a clone of the repo and adds a git remote for his production server.

The first thing that you need to setup is the bare repository on your web servers. Set up your empty project and repo folders with the correct user permissions. Here are my normal paths:

Now that you have a bare repo you can add your post receive hook and set up the split work tree folders. Having a split tree repository is a little more complicated than having a normal GIT clone but it is what makes having this server as a GIT remote possible. This also keeps your .git folder outside of the live web server directory.

Now inside the repository folder you will have a hooks folder. Create a new file in that folder called post-receive. The post-receive hook will get called and executed every time a new push is made to the repository. Here is the what should be in the file. Change your paths accordingly.

Now we need to add this repository as a remote on the machine you want to push with. In a multi-server environment that would be your CI server or build machine. On a small single server environment that may be on your development machine. Either way the user needs to have access/permissions to the files and the appropriate ssh keys/credentials to make the connection. Here is how to setup the remote on that CI/developer machine. Go to the projects got checkout that you cloned. Now add the remote, we will call it prod:

Now that the remote is setup test out the SSH connection then do your first push.

Now the production push:

In a multi server environment you would then push to all of the remotes. We use shell scripts to run through the remotes and clear our caches etc.

There you have it! This is a super easy setup to reliably push code out to your production and staging servers. The post-receive hook can contain any of your other commands in it like npm install or gulp/grunt commands.

Happy Deploying!