How to Create a Local Git on your Cloudways server?

Although, Cloudways platform has made it easy to deploy web applications without going through the process of specific server configuration, making the process cost-efficient and flexible with their “Deployment via Git” feature. Most customers would want to push their work directly to their servers without any involvement of third-party version control systems, like GitHub, GitLab, BitBucket, etc. There could be various reasons for this, like privacy, security or just having total control over their work.
In this guide, I will be showing you how to setup Local Git on your Cloudways server.
Step 1 — Creating and Initializing Git on Local Computer
First of all, create a directory on your local computer and initialize git. This directory will be your working directory, from where you are going to push changes to your Cloudways server.
$ mkdir myproject
$ cd myproject
$ git initStep 2 — Creating and Initializing a bare Git repository on your Cloudways server.
Now log into your Cloudways server. Note that we do not want to put our main git repository under our webroot, which is “public_html”.
Create another directory inside “public_html”. This directory will be your main git repository.
$ cd public_html
$ mkdir myproject.gitNow initialize a bare git repository inside “myproject.git”.
$ git init --bareWhat is git bare repository you might ask?
A bare Git repository is a non-working repository. In a regular git repository, you can see a “.git” hidden directory. This directory is called the brain of that particular repository. This directory takes care of the changes made, commits, etc. When you initialize a Git bare repository, it consists of all the contents which are inside the “.git” folder. Git bare repository is basically a sharing repository among a team of developers.
Step 3 — Adding a remote repository on your local computer.
Now let’s go back to our local computer, inside “myproject” directory and add a remote repository. For this, you will have to create a URL to your “myproject.git” repository on your Cloudways server.
$ git remote add origin [username]@[server_ip-address]:applications/[application_name]/public_html/myproject.gitStep 4 — Using Git Hooks Post-Receive
Now, you must be wondering, this is not going to update our webroot folder i.e. “public_html” because the files are just gonna be stored in “myproject.git”, right? Right!
This is where Git Hooks come in handy. Git Hooks are shell scripts that trigger when you perform a specific action in Git. Git hooks exist as simple text files inside your main bare git repository.
Go inside “myproject.git/hooks” and rename any of the files inside to “post-receive”. Open the “post-receive” file and replace all its content by pasting the below script.
#!/bin/sh
git --work-tree=path_to_public_html --git-dir=path_to_myproject.git checkout -f name_of_branchThe above script will push the changes to the “public_html” folder as soon as “myproject.git” receives the push from the local computer.
And Voila!!
Now you can push your changes directly from your work directory on your local computer to your Cloudways server.
Step 5 — Testing
You can test your changes by creating and adding a file. Make a commit and push to your Cloudways server.
$ nano index.php
$ git add .
$ git commit -m "adding index.php"
$ git push origin master