How to deploy your application to shared hosting with GIT

How to make Continuous Deployment (CD) to work on your shared hosting

Wouldn’t it be great if you can deploy your website into the cloud with just a single push?
Yes absolutely.
This tutorial will just teach you how to do this.

It’s one of the best things I’ve learned to do with git and it’s making my deployment damn quicker and fast.

Using git for deployment makes it very very easier for us to deploy our websites/web applications to the cloud in an easy and quick way. If something is broken in the production, we can also easily revert to the previous build.
There are advantages such as,
-Managed version control
-Quick Deployment
– Quicker release management
– Easy to setup

Initially, you need to have a web hosting with ssh access.

At first, Connect to the server with ssh. If you are in windows, use putty, or in Linux/mac just use ssh in your terminal.

I am using a2hosting for this tutorial. A2hosting is the best-shared cloud service provider I’ve found till now.
open your terminal and hit these commands,
In a2hosting you can get all these credentials and ports in your dashboard.
ssh -p 7822
now, it’ll ask for credentials and you can hit the password displayed on the homepage of your hosting provider.
The default port for ssh is set to 7822 in a2hosting.

 After a successful connection, go to the root of your cloud server and create the bare repo there.

git init –bare ~/project.git

Enter the following commands in a terminal:

cd ~/project.git/hooks
nano post-receive

copy and paste the following code exactly as it is. Please enlarge the terminal before pasting to avoid cut out texts.

TARGET=”/home/username/” –your domain’s home folder
GIT_DIR=”/home/username/project.git” –your git repo URL

while read oldrev newrev ref
# only checking out the master (or whatever branch you would like to deploy)
if [ “$ref” = “refs/heads/$BRANCH” ];
echo “Ref $ref received. Deploying ${BRANCH} branch to production…”
git –work-tree=$TARGET –git-dir=$GIT_DIR checkout -f $BRANCH
echo “Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server.”

Giving execute permission to post-receive script.

chmod +x post-receive

It’s all we need to do.
After that whenever we push to this repo, our contents would be copied to the target website’s home folder.

Now in the client PC,
Go to your project’s distribution folder and open terminal & paste the following code.

git remote add production ssh://

and common git commands:
git add .
git commit -m “initial changes”
git push origin master
and finally…

git push production master

After doing this your codes would be pushed to the production server.

It’s very easy to set up and also very easy to deploy to the cloud using this method.

If you faced any issues please let me know in the comment section below.
Thank You.

Back To Top