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).
- Create a new directory for your git repository (preferably outside of your www directory):
12$ mkdir -p /home/git/project_name.git$ cd /home/git/project_name.git
- Create a bare (empty) git repository inside of your new directory:
1$ git init --bare
- 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:
123$ cat > hooks/post-receive#!/bin/shGIT_WORK_TREE=/var/www/project_name git checkout -f
/var/www/project_namewith 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.
- Make your post-receive hook executable:
1$ chmod +x hooks/post-receive
Setting Up Your Local Server
The following instructions apply to your local (eg: Development) server.
- Navigate to wherever your git repository is located:
1$ cd /var/www/project_name
- Add a remote source that references the server and repository we setup in the previous steps:
1$ git remote add production ssh://website.com/home/git/project_name.git
You can use whatever you want in place of
website.comwith your server address, and be sure that
home/git/project_name.gitmatches the directory you created in step 1.
ProTip™ – Add your username to the url so you don’t have to type it each time:
- 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
masterin the following command:
1$ git push production +master:refs/heads/master
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:
$ git push production master
If your branch isn’t named master then use your branch name instead. The main point here is the format of the command:
$ git push <remote server> <branch name>
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
$ mkdir -p /home/git/project_name.staging.git
$ cd /home/git/project_name.staging.git
$ git init --bare
$ cat > hooks/post-receive
GIT_WORK_TREE=/var/www/project_name git checkout -f
$ chmod +x hooks/post-receive
$ cd /var/www/project_name
$ git remote add production ssh://staging.website.com/home/git/project_name.staging.git
$ git push staging +staging:refs/heads/staging
$ git push staging staging
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: