How to deal with Git on our shared servers


1. Overview
2. Git installation
3. Creating a local repository and committing
4. Working with the remote repository hosted at a Namecheap shared server


1. Overview

Git is a distributed version control system. While it may sound confusing, it helps to address a number of important and topical problems, such as:

  • having multiple versions of one file or a group of files edited by a group of people with an ability to save all versions - from early ones to latter ones
  • avoiding of important files overwriting
  • tracking revision history (each change is prompted to be documented)

With Git, it’s not necessary to manually back up file copies before saving them.

For example, having a group of files in your production folder with different names but similar functions, such as:
  • .htaccess
  • .htaccess_backup
  • .htaccess.bp
  • .htaccess_mary
  • .htaccess_secured
  • .htaccess2015-06-12
will only end in a disaster.

With implementing git version control system, all of your changes will be under control. Originally, it was created to manage the Linux kernel source code, and nowadays, it is in active use by many developers globally.


2. Git installation

Git is pre-installed on our shared servers. The absolute path to the executable file is as follows:

/usr/local/cpanel/3rdparty/bin/git

You may need to install it on your local computer as well. Our recommendation is to use following pieces of software:
using such commands as:
  • sudo yum install git for Fedora and CentOS
  • sudo apt-get install git on Debian-based systems, such as Ubuntu.
You can find more details here.

3. Creating a local repository and committing

The most usual workaround for using Git involves having a number of local remote repositories (on developers’ computers) and one remote repository used to share code between a group of developers and move things to production at some point later.

With a local git repository a developer may track his/her own changes and prepare to committing to a shared remote repository a concise piece of code.

In order to create a local Git repository, create a directory first (using the mkdir shell command, for example). Then, change the directory to the newly created one ( the cd command may come in handy) and use the git init command:




Next, you’ll need to find files you want to be controlled to the version control index. The command to do that is:

git add <file> such as:
git add example.php

Multiple files should be separated with spaces:

git add example.php sample.txt readme

If you want to add all of the files in the directory, you may use a shortcut:

git add . ( with ‘.’ at the end):




In order to commit the file change to a local repository, the following command is to be used:

git commit -m “commit comment”, like this:
git commit -m “test commit”




4. Working with the remote repository hosted at a Namecheap shared server

Our recommendation is to manage a remote Git repository over SSH. It provides a considerable level of security, convenience and integration. You may request SSH access granted for your shared hosting account with the help of our Support Team.

First of all, specify your path to be extended to with the /usr/local/cpanel/3rdparty/bin directory where Git executables are located so that it wouldn’t be necessary to type the full path each time Git is addressed.

To do that, type the following command into your SSH command prompt while connected to your hosting account over SSH:

nano .bashrc

A GNU Nano text editor screen will appear. Append its contents with the following lines:

PATH=$PATH:/usr/local/cpanel/3rdparty/bin
export PATH







After that, press CTRL+X , then press Y and Enter to save changes. In order for the changes to be accepted it will be necessary to re-log into your SSH command prompt.

Now, you may create a repository the same way it was done locally. Once done, we are ready to get local changes pushed to the remote repository.

You may create an alias for a remote repository on your local device for your convenience:

git remote add origin ssh://<username>@<servername>:21098/home/<username>/public_html/<directory_of_site>

where:

  • <username>  should be replaced with your cPanel username;
  • <servername>  should be replaced with your server hostname (click here to find out how to check a server hostname)
  • /public_html/<directory_of_site>  should be replaced with the path to your remote repository





After that, you may push changes to the remote repository with the help of the following command, while in the local git repo directory:

git push -u origin master





or its full alternative if no aliases/shortcuts were used:

git push --receive-pack=/usr/local/cpanel/3rdparty/bin/git-receive-pack ssh://<username>@<servername>:21098/home/<username>/public_html/<directory_of_site> master


That's it!

              
                      Need any help? Contact our HelpDesk

Comments

We welcome your comments, questions, corrections and additional information relating to this article. Your comments may take some time to appear. Please be aware that off-topic comments will be deleted.

If you need specific help with your account, feel free to contact our Support Team. Thank you.

Need help? We're always here for you.

× Close