How To Set Up Automatic Deployment with Git with a VPS


For an introduction to Git and how to install, please refer to the introduction tutorial.

This article will teach you how to use Git when you want to deploy your application. While there are many ways to use Git to deploy our application, this tutorial will focus on the one that is most straightforward. I assume you already know how to create and use a repository on your local machine. If not, please refer to this tutorial.

When you use Git, the workflow generally is toward version control only. You have a local repository where you work and a remote repository where you keep everything in sync and can work with a team and different machines. But you can also use Git to move your application to production.

Server Setup

Our fictitious workspace:

Your server live directory: /var/www/html

Your server repository: /var/www/site.git

What should we do if we want to push to site.git and at the same time make all the content available at /var/www/html?

To create our repository:

On /var/www/site.git we execute sudo git config –global init.defaultBranch main and right after sudo git init –bare

You will see a few files and folders, including the ‘hooks’ folder. So let’s go to ‘hooks’ folder:

cd hooks

sudo touch post-receive
sudo nano post-receive

git –work-tree=/var/www/html –git-dir=/var/www/site.git checkout –force

sudo chmod +x post-receive


git remote add production ssh://root@ip-address/var/www/site.git


(take ownership)———————————

sudo chown -R useranme /var/www/repo

(change permissions) —————————

sudo chmod -R 775 /var/www/repo

Free Web Hosting