WEB HOSTING SUPPORT NETWORK
     
 

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

Note: Instead of mywebsite.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).

Note: If you already have a repository for your website, and it was originally created on GitHub after October 2020, you should add the following to the end of the initialization command: "--initial-branch=main".

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

Note: 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@your_server.com:/home/example/private/mywebsite.git

Note: The name of the remote is production. You should replace example with your Control Panel/SSH username, your_server.com with the hostname of your server and /home/example/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.

Note: If your original repository was created on GitHub after October 2020, you should replace master with main.