Setting up a Git Server on Hostgator

Submitted by Tom Thorp on Thursday, July 13, 2017 - 16:12
Modified on Wednesday, August 1, 2018 - 02:48
Drupal Icon With Git
As a Drupal web developer, I had been accustomed to transferring my development projects to production by transferring a full copy of my source and database to my production environment whenever i apply any changes. It is often time-consuming, and can be problematic if not all steps are done in order. There had to be a better way.
Enter, Git. 
Git is a Change Management System designed to manage and track changes to your projects. It can be used to stage changes to your software, whether it be adding a new feature, testing a patch, handing over code to another team member, or committing to production. It's possibilities are only limited by how you have configured your development environment. 

Set up key-based authentication

In order to set up Git so that you aren't prompted for credentials each time you connect to Hostgator, you have to establish key-based authentication between your computer and your Hostgator account. To do this, you have to transfer your public key (~/.ssh/ to Hostgator. If you have not established a public key on your computer, run the following command: 
$ ssh-keygen -t rsa
Upload your public key to Hostgator using the following commands :
$ scp -P 2222 ~/.ssh/ \
$ ssh -p 2222 \
> 'chmod 600 ~/.ssh/authorized_keys'
Finally, on your computer, add the following to ~/.ssh/config :
  Port 2222
  PreferredAuthentications publickey
You should now be able to login to your Hostgator account using key-based authentication using port 2222 as the default.
$ ssh

Initializing your Git Server

Once you've logged in, set up a directory on Hostgator where your Git server is going to live. eg.
$ mkdir ~/gitrepos/mywebsite.git
$ cd ~/gitrepos/mywebsite.git
$ git init --bare
This initialises your Git server in the mywebsite.git directory.

Setting up Post-Receive hook on Git Server

A Git Server's main function is to push/pull changes according to the instructions it receives. However, it can only work if it knows where the locations of both the Git Server and your project are. 
In the hooks directory in the Git Server mywebsite.git, create a file called 'post-receive' and enter in the following : 
while read oldrev newrev ref
    if [[ $ref =~ .*/master$ ]];
        echo "Master ref received. Deploying master branch to production ..."
        git --work-tree=<full path to root directory of project> \
             --git-dir=<full path to Git server> \
            checkout -f
        echo "Ref$ref successfully received. Doing nothing: only the master branch may be deployed on this server."
Save the script, then type 'chmod +x post-receive' to make the script executable.
The script serves two purposes. First, it tells Git whereabouts where the project directory (--work-tree) and Git server (--git-dir) are located. Second, it only allows changes you've made to your project to be pushed through to your production environment, if your changes had been merged into the master branch first. A good safety precaution to prevent a development version of your project from finding it's way into production. 

Configuring Git on the Client Side

Initialise your Git repository on your client, by changing to the root directory of your project. Then type :
$ git init
This will create a '.git' folder. 
Change your directory to .git/hooks, then create a file called 'post-commit' with the following :
git --work-tree=<full path to local project directory> \
--git-dir=<full path to local Git repository> \
checkout -f
The 'post-commit' hook runs after the commit on the client completes. 

Configuring the Remote Repository

In order to push our changes to the Git server on Hostgator, we need to configure Git where our remote repository lives. To do that, from within your project managed by Git, enter the following :
$ git remote add production \
> ssh://<account username>@<domain name>:2222/<path to Git Server>
If you are on a shared service, your path will most likely be prefixed with the tilde '~' character to signify your home directory. 

Testing your Git configuration

To test your configuration, from within your project do the following:
$ git checkout -b test_feature
$ echo "<h2>Hello World</h2>" >> test.html
$ git add .
$ git commit -m "Git Test"
What we have done is created a new Git branch called 'test_feature'. Any changes made to your project from this point forward will only be reflected in this branch. We then created a file called 'test.html' and appended some text to it. We then added the file to the Git repository to track any changes, then we finally committed the changes on the branch to the Git repository. 
Now we have to test pushing the changes up to the Git Server on Hostgator. 
$ git push production test_feature
This will instruct Git to push the changes you made on branch 'test_feature' to Hostgator. However, when you inspect the directory on Hostgator, you'll notice that the file 'test.html' did not get created. That is because the 'post-receive' hook that's configured on the Git Server, will only push changes that originate from the 'Master' branch. It will create a branch on the Git Server called 'test_feature' with the changes, but it won't commit those changes to the main production environment.
To push those changes through to the 'master' branch, you first have to merge those changes you made in the 'test_feature' branch, into the 'master' branch. 
$ git checkout master
$ git merge test_feature
Finally, push the master branch to your Git Server on Hostgator. 
$ git push production master
Check your file system on Hostgator to see that the changes went through correctly. 

About the author

Tom Thorp
Tom Thorp is an IT Consultant living in Miami on Queensland's Gold Coast. With over 30+ years working in the IT industry, Tom's experience is a broad canvas. The IT services Tom provides to his clients, includes :
Website development and hosting
Database Administration
Server Administration (Windows, Linux, Apple)
PABX Hosting and Administration
Helpdesk Support (end-user & technical).