Using Git to Deploy Code

by Mike Everhart on January 17, 2013 in Git with 13 Comments

Like many other programmers, I use git on a daily basis to manage my code. In fact, I believe git is so useful that I list it as a required skill whenever I post job openings for programmers at work. Although it’s used by a lot of people, I often find that git is not utilized to its full potential. Mainly, I find that programmers will use git to version their code, but they still upload files through FTP or some other method. This article is going to show you how I use git to quickly and easily deploy code across multiple servers without using FTP.

Setting Up Your Remote Server

The following instructions apply to your remote (eg: Production) server(s).

  1. Create a new directory for your git repository (preferably outside of your www directory):
  2. Create a bare (empty) git repository inside of your new directory:
  3. Create a post-receive “hook”. This hook gets executed automatically each time you push code changes to the server. In our case, we’ll use the hook to checkout the latest code from our repository:

    Update GIT_WORK_TREE, replacing /var/www/project_name with the root directory of your project (where you would normally upload the files using FTP).

    ProTip™ – You can add other shell code to the post-receive hook to do things like chmod files and directories, call other scripts, etc. each time you push updated code to the server.

  4. Make your post-receive hook executable:

Setting Up Your Local Server

The following instructions apply to your local (eg: Development) server.

  1. Navigate to wherever your git repository is located:
  2. Add a remote source that references the server and repository we setup in the previous steps:

    You can use whatever you want in place of production (such as staging or server1). Replace with your server address, and be sure that home/git/project_name.git matches the directory you created in step 1.

    ProTip™ – Add your username to the url so you don’t have to type it each time: ssh://

  3. Do an initial push to the remote server, which will setup the master branch. If your branch is named something other than master then use its name in place of master in the following command:

Deploying Code

Each time that you make changes to your code commit them as usual (using $ git commit ... , etc.), and then run the following command to deploy your changes to the remote server:

If your branch isn’t named master then use your branch name instead. The main point here is the format of the command:

And that’s it! Now you can easily deply code across multiple servers using only a few commands.


Multiple Servers and/or Branches

If you have multiple servers and/or branches (such as development, staging, and production) then you’ll need to repeat all of the steps for each one. Here’s the same steps from above, only using staging for the server and the branch in place of production (server) and master (branch):

Remote Server

Local Server



It’s important that file permissions are set so that the Apache (or whatever server you’re using) user can write to the directory that you defined as GIT_WORK_TREE. If not, then the files can’t be pulled from the repository and placed in the correct directory. This is especially true if you created your repository outside of your root www directory (as recommended).

Changing Remote Server Settings

If you ever need to change the information for one of your remote servers, the easiest way is to edit the git config file. On your local server, open the config file located inside of your git repository (.git/config). By default, the config file is pretty bare, so you won’t have any trouble finding what to edit:

I'm a programmer, web developer, and owner of PlasticBrain Media LLC. I have a passion for startups and creating web-based applications that help them analyze, streamline, and automate their business processes. I have 15 years of experience in standards-based web design, programming, SEO, and SaaS.

Leave a Reply

Your email address will not be published. Required fields are marked *