Using Git to Deploy Code

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.

    Create /home/git/project_name.git/hooks/post-receive and add the following:

    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 commands and 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:

6 responses to “Using Git to Deploy Code

  1. Hi Mike, thanks for the great article!

    I have set up the following on bluehost server.

    /public_html/ (base code of drupal multisite reached with and 2 associated subsites

    /public_html/bizness (another base code of a second multisite reached with and 2 associated subsites

    I would like to use git, but not sure if I set up repository for which will encompass additional subsites and another for which will also encompass those subsites, or if each subsite needs a repository.

    What is, in your opinion, the most efficient git workflow to manage these sites?


    1. Hey Mark,

      Thank you for the comment. Based on what you described, I recommend having a separate git repo for each project/client/sub site. So, in your case you would have four repos:

      1. client1.markyoung
      2. client2.markyoung
      3. client1.bizness
      4. client2.bizness

      On your server create a directory for each one like /home/git/client1.markyoung.git, /home/git/client2.markyoung.git, and so on. You should then be able to follow the rest of the steps in this article as normal to setup each repo.

      1. Thanks Mike,
        I will get working on this. A git workflow seems like the best workflow to use.
        If the configuration works out, I will let you know.
        Take care

Leave a Reply

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