Setting up Git on a virtual hosting

Setting up Git on a virtual hosting

11:06, 01.06.2022
Author: Artur Berezhnoy
2 min.
63

The distributed version control system Git is often used on hosting sites in order to simplify the updating of the site during development. Without this tool, you will have to download new versions manually, which takes more time. Having configured Git, you will be able to update everything automatically by pressing a single button. Later in the article, we will tell you how to connect Git to hosting and prepare it for work.

Step one: Create a repository in cPanel

Open the control panel and go to the "Git Version Control" tab, it is located in the "Files" section. Here we need to create a repository by filling in all the fields below. It is important that the folder where the files will be stored is empty, otherwise the system will not allow you to use it as a repository.

Step two: Upload the changes from the remote repository to the hosting

Next, as soon as you need to update the site, go to the Git "Manage" section, select the repository you created earlier and initiate the update procedure. As a result of these actions, files located in a remote repository on the version control system website will be uploaded to your local server.

Here you can study information about all the recent periods of activity – this is useful during development and helps to identify the time point when certain problems with the site began.

setting up git on a virtual hosting

Step three: Setting up automatic site updates from the repository

To automate the update process, you need to create a file called .cpanel.yml in the root folder of the repository. Open it in edit mode and add the following lines:

---
deployment:
tasks:
- export DEPLOYATH=/home/your_user in_cpanel/path_to_repository
- /bin/cp -R folder_name_in_repository $DEPLOYATH

- /bin/cp file_name_in_the_repository $DEPLOYATH

In the line "export DEPLOYATH", be sure to specify the correct path to your repository, otherwise nothing will work. Next, if you need to specify several folders here at once, they should be registered separately, from a new line. After making all the changes, save the file, it is not necessary to restart the server.

How to manage a repository via the console

After the work done above, you will be able to update the site files in a couple of clicks through the cPanel control panel, but this option is not as convenient for many as through the console. Therefore, we recommend using a secure connection via SSH protocol, and already through it to manage updates. Here are the commands you need:

  • git checkout branch_name – go to the specified branch of the remote repository. By default, cPanel will only work with the master branch.
  • git pull – does the same thing as the "Update" button in cPanel.
  • git log --all --decorate --oneline –graph – allows you to study the commit history.
  • git revert commit id – rolls back the local cPanel repository to the specified commit.
  • git clone ssh://link – downloads the current code to the computer.

That's basically all you need to know about setting up and connecting Git repositories on hosting. If you have any questions, please contact our specialists, we will be happy to help within our competence.

Related Articles