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
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 email@example.com $ mkdir example.com.git $ cd example.com.git $ 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 th
e 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 #!/bin/sh GIT_WORK_TREE=/home/username/public_html/example.com/ export GIT_WORK_TREE 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 example.com ssh://firstname.lastname@example.org:2222/home/username/example.com.git $ git push example.com +master:refs/heads/master
After you run the push with “
+master:refs/heads/master“, for subsequent updates you only have to do:
$ git push example.com
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.