Search the blog

git is a great way to record the history of your project. Pushing the code remotely also acts as a way of backing the code up and sharing it with other people. cPanel now has a git module. It does not support HTTPS though, only SSH, so you will need to check with your hosting company first to see if you have access.

Assuming you do, this post will show you how to set up a remote repository that publishes your code when you push to it. It will also give you web-based access to view your repository in cPanel.

I will assume that you already have a local git repository and SSH access. In the following commands you will need to substitute cpaneluser for your cPanel user name and domain.com for your domain.

While on the subject of SSH access I recommend you set up a SSH key so you don’t have to enter your password each time. Breifly, this is how you can do it on MacOS. I personally password-protect my key with a secure password and reuse it across servers. Since my private key is only ever stored password-protected on encrypted hard disks this is secure.

# On your local machine
ssh-keygen -t rsa; # Follow instructions, password recommended
chmod 600 $HOME/.ssh/id_rsa;
ssh-copy-id -i $HOME/.ssh/id_rsa.pub cpaneluser@domain.com; # Copy the public key to the server
ssh-add -K ~/.ssh/id_rsa; # Store in your keychain so you don’t need to keep entering it

Set up the git repository

Create a repositories folder outside the web root. If you can’t do this outside the web root protect the git folder and other associated files with .htaccess.

# Remote server
mkdir respositories;
cd repositories;

Create a “bare” repository. A bare repository does not store a checked out copy of the code and traditionally ends in .git. Repositories on Github and Bitbucket work in this way.

# Remote server
git init --bare foo.git;

Create a file locally called post-receive in repositories/foo.git/hooks/ with the following contents. The --work-tree path should be set to your site root and the --git-dir path should be set to your repository.

#!/bin/sh
git --work-tree=/home/cpaneluser/public_html/foo.co.uk --git-dir=/home/cpaneluser/repositories/foo.git checkout -f
This will checkout the current branch. If you want to explicitly checkout a branch each time you can add the branch name to the end of the command (E.g. checkout -f dev). Also note that depending on your hosting you may need to change the first line.

Upload the file to your remote server and make it executable.

# Remote server
cd foo.git/hooks;
chmod 755 post-receive;

Add your new remote.

# Local server, in the repository folder
git remote add origin ssh://username@domain.com/home/cpaneluser/repositories/foo.git

Now when you push to this remote the post-receive script runs in the bare repository, which checks the code out to your site root.

Add the repository in cPanel

Now in cPanel you can browse your code and commits — and you can push files and publish them at the same time.

This should show you the principle of how this works and you can adapt it to suit your workflow. You can set up additional repositories as well (e.g. have one for development and another for production). Depending on how you use branches you may wish to set the command in post-receive to checkout a specific branch on each repository as mentioned above.

Tim Bennett is a freelance web designer from Leeds. He has a First Class Honours degree in Computing from Leeds Metropolitan University and currently runs his own one-man web design company, Texelate.