How to Use Git with Your Web Hosting

You can use Web Hosting’s git service to upload your source code and deploy your application, including installing dependencies, to your web hosting.

Your web hosting’s Control Panel also has a web-based graphical interface to browse your git repository.

Learn all about using the Web Hosting git service in this article.

Prerequisites

You’ll need to have git and an SSH client installed on your computer in order to use the git service.

Overview

Web Hosting’s git service hosts remote git repositories for you. Additionally, it allows you to build and deploy your application on your web hosting, installing any dependencies you declared.

On PHP web hostings, every Site has its own independent git repository, named after the domain that corresponds to the site (ex: example.com.git.) On Node.js and Python web hostings, there is only one git repository, which is named default.git.

After adding the repository as a git remote to your project directory, you’ll be able to git push your code to the git service. By default, the service will expect you to use the master branch, but you can push any branch or tag that you wish.

You can also git clone a repository from our git service and use any other features you’d expect from a remote git server. You can truly use it as a version control system.

Once the files have been pushed to the service, they become available for deployment with the deploy {repository}.git command, which is executable via SSH on the same address as the git service.

The deploy command will build your application and copy the application files to your web hosting. Application dependencies can be declared using the supported package managers for each web hosting type and will be installed as part of the deploy process: Composer for PHP, NPM for Node.js, and pip for Python.

All these steps are detailed in the sections below.

git Service URL

In the examples that follow, the git service URL is represented by a $GIT_URL bash variable. You’ll find the actual URL in your web hosting management page.

On PHP web hostings, you’ll find it in the “Sites” section within each Site’s page.

On other web hostings, you’ll find it the “Deploy” section of your web hosting’s management page.

The format of the git service URL is always as follows:

GIT_URL="ssh+git://{web_hosting_id}@git.{datacenter_id}.gpaas.net"

Authentication

You can authenticate on the git service using your web hosting’s password (default behavior) or public SSH keys.

If you choose to use your web hosting’s password, you’ll need to provide your password whenever you interact with the remote git service, for example to perform git clone, git push or deploy commands.

When using SSH key authentication, your system should be able to provide the correct public key to the git service whenever necessary. This is the recommended way to authenticate, as it is convenient, secure and enables you to write automated scripts.

Create a Repository

Here are a couple of examples to create your repository for all web hosting types: PHP web hostings, where each Site has its own repository, and other web hostings, where only a default respository exists.

Once the repository has been created, you will be able to see it in your web hosting’s Control Panel using gitweb, an open source web-based graphical user interface (you will need to reboot your web hosting after you create a repository for the first time on your web hosting).

Example for a PHP Web Hosting

This example shows you how to create a repository locally to upload code to a PHP web hosting. It uses a Site called example.com. You can simply replace the site’s domain with one your domains, including the test domain that was automatically created for you.

$ mkdir example.com
$ cd example.com
$ git init
$ git remote add gandi $GIT_URL/example.com.git
$ mkdir htdocs
$ echo "Hello world" > htdocs/index.html
$ git add htdocs
$ git commit -m "first version of index.html"
$ git push gandi main

On PHP web hostings, the root of your repository is not the directory used by the Apache httpd server to serve files to web visitors. Consequently, in order for your files to be accessible via the web, you must put them in the htdocs/ directory, to be created at the root of your repository.

Example for Other Web Hostings

This example shows you how to create a repository on a Node.js or Python web hosting using a dummy text file. Please refer to their respective documentation pages to learn how to structure your project files.

$ mkdir myapp
$ cd myapp
$ git init
$ git remote add gandi $GIT_URL/default.git
$ echo "Creating my repository" > test.txt
$ git add test.txt
$ git commit -m "Add a text file"
$ git push gandi main

Clone a Repository

Copy a remote repository to your computer using the git clone command:

$ git clone $GIT_URL/{repository}.git

By default, the git clone command will create a directory with the same name repository (ex: example.com or default) and copy files into it. If you want to specify another name for the local directory, without interfering with any git settings, simpy add it to the command:

$ git clone $GIT_URL/{repository}.git my_app

Deploy Your Code

This command will perform a git checkout in the target directory on your web hosting. It will also install any dependencies using the packager manager: Composer for PHP, NPM for Node.js, and pip for Python.

$ ssh {web_hsoting_id}@git.{datacenter_id}.gpaas.net 'deploy {repository}.git'

Remember that the {repository} placeholder corresponds to a Site’s address on PHP web hostings (ex: example.com), but is always default on other web hosting types.

The deploy command assumes that you want to deploy the code checked into your repository’s master branch. If you wish to deploy another branch or tag, for example a production branch, you can add it to the end of the command:

$ ssh {web_hosting_id}@git.{datacenter_id}.gpaas.net 'deploy {repository}.git production'

Note

The use of Git submodules is not supported at the moment.

Dealing with existing files

Any directories already present in the target deployment directory must at least have the same write access of the user, so that the files can be replaced during the deployment (ie. chmod 644). This action is to be carried out if you encounter an error like:

error: unable to unlink old 'htdocs/sites/default/settings.php' (Permission denied)

Additionally, you may want clean up your target directory. The following command will execute a git clean operation on your directory and delete any files that are not tracked by git or not listed in your repository’s .gitignore file:

$ ssh $GIT_URL 'clean {repository}.git'

Warning

Keep in mind that if your web application has user-upload directories, file-caches, or untracked configuration files that you want to keep on the server (ex. environment specific configurations / secret keys), those should be added to your repository’s .gitignore file prior to running the clean command.

Delete a Repository

To completely remove a repository from your web hosting, delete its directory via sFTP or the Emergency Console.

The directory’s path via sFTP looks like this:

/lamp0/vcs/git/{repository}.git

Via the console:

/srv/data/vcs/git/{repository}.git

Windows Users

Windows users can use the git for windows client.

SSH Key Fingerprint

The SSH key fingerprint for the git endpoint is:

SHA256:BvMQxzqvnTLvYLa0s+11xy45lQLXxGmXItHtKGXg5yA
MD5:f1:93:f8:eb:5e:b1:90:e7:1d:5e:39:d5:34:43:9f:cb

Resources