90% OFF ALL NEW PLANS TO HELP MORE BUSINESSES MOVE ONLINE

Using Git to push changes to your live website

 This tutorial provides instructions how to set up a Git remote repository on your hosting server so that you can easily deploy the latest version of your website files.

Prerequisites

  • SSH access is enabled in the SSH Access section of the hosting Control Panel.
  • Git is installed on your computer.

Example Workspace

In this tutorial, the Control Panel and SSH username is ‘example’. Therefore, the default root directory for the domain is /home/example/www/www. The server hostname is sXXX.sureserver.com. You should replace these values with the actual values for your account.

Set up remote Git repository

1. Connect to the server for your account via SSH. You can follow the instructions in our online manual.

2. The repository should not be accessible via the web. Therefore, you should create it in the private directory on your account. Go to the private directory:

cd ~/private

Create a directory for the repository and enter that directory:

mkdir mywebsite.git
cd mywebsite.git

Instead of website.git you can use any name you like but it should end with .git.

Create a bare repository and initialize it:

git --bare init

A bare repository is a repository that is created without a working tree (e.g. the actual files of the project).

3. In order for the files to be pushed to the correct location, you need to create a post-receive hook:

nano hooks/post-receive

Add the following to the file and save it:

#!/bin/sh
GIT_WORK_TREE=/home/example/www/www git checkout -f

You should replace /home/example/www/www with the location of your live website files. The directory must exist.

Give the file executable permissions:

chmod +x hooks/post-receive

On your local computer

1. If you do not already have a repository for your website on your computer, you can initiate a new repository and add all files in the current directory with the following commands:

git init
git add .

You should also add your first commit:

git commit -m "My website is ready"

2. Now you can add a remote to the repository:

git remote add production example@sXXX.sureserver.com:/home/example/private/mywebsite.git

The name of the remote is "production". You should replace 'example' with your Control Panel/SSH username, sXXX.sureserver.com with the hostname of your server and /home/private/mywebsite.git with the location of the git repository.

3. Finally, you can push the website from your local computer to the hosting server:

git push production master

This command instructs Git to push the current master branch to the production remote.