Using git to Remotely Install Website Updates

There’s a great recipe for this at Using Git to manage a web site. I thought I’d share my version of it, just slightly modified from the original and HostGator-centric. Even if you’re using shared hosting, you may still be able to use git to remotely install updates to your sites.

I was pleasantly surprised to discover that HostGator DOES include git in it’s shared hosting packages. You’ll need ssh access for this recipe to work — an extra hoop for HostGator customers using shared hosting.

Create A Remote Repository

First, we’ll ssh to our hosting account and create a bare git repository in a location outside the public HTML folder (e.g. /home/username/public_html). I’ve named the parent folder of the git repository the same as the domain name + “.git” to keep things simple as I have multiple websites managed this way using git.

$ ssh -p 2222
$ mkdir
$ cd
$ git init --bare

Create a Hook

Git hooks are scripts that are executed when specific actions are performed to our repository. In our case, we’ll want the post-receive hook to be executed when we push changes from our local repository. This script will instruct git to use a different working folder for our files within the public_html folder, then checkout the updated files to that location.

$ cd hooks
$ cat > hooks/post-receive
git checkout -f
$ chmod +x hooks/post-receive

I needed to export the GIT_WORK_TREE variable before the checkout. This was the only modification I had to perform on the original script.

Add a Remote Repository

Now, at your local repository (for me, it’s my laptop), you’ll need to add the remote repository so you can push to it.

$ git remote add ssh://
$ git push +master:refs/heads/master

After you run the push with  “+master:refs/heads/master“, for subsequent updates you only have to do:

$ git push

The Wrap Up

That’s basically it! Now instead of using FTP to get your updates to the server, you can use secure shell and git version control. If you find that an update isn’t working on the production server, you can quickly login and, using git, roll your changes back to the previous version without the hassle.

10 comments on “Using git to Remotely Install Website Updates

  1. I’m having an issue with this.. works perfectly, but the files that are pushed are set to 664 or 775… I get an internal server error, because my codeigniter index.php file has to be 755. Any solution for this?

  2. I am using apache server

    but i am not understand:

    you using this:

    I am using:

    i dont understand the ‘home’ dir here. 🙁

Leave a Reply