Skip to content
Rashid Azar

Deploy your Website Changes using Git

Linux, Git1 min read

It is really a bad idea to deploy website using FTP and replacing old files with new one. Let me explain you the deployment using git which makes the life easy.

Before starting we need SSH client and Git to be installed on our local machine. I am assuming you already have installed these applications. So lets begin the interesting stuff.

We need to install Git on the remote server, so SSH to the server and issue following command:

1$ sudo apt-get install git

Now we need to create a bare Git repository which should located outside the webroot. We can keep it in our home directory or create a separate folder in the home folder for bare Git repositories.

Lets create out Git repository

1$ mkdir myapp.git
2$ cd myapp.git
3$ git init --bare

Now we are going to create a hook which will move latest changes in the webroot. Create a file hooks/post-receive and paste following code it it:

1#!/bin/sh
2GIT_WORK_TREE=/path/to/webroot/of/mywebsite git checkout -f

Update the GIT_WORK_TREE path with your webroot path. Make this file executable:

1$ chmod +x hooks/post-receive

Server setup is done.

On your local machine you just need to add a remote branch to push your changes.

1$ git remote add server remoteuser@server:/path/to/git/repo
2$ git push server master

Your latest code is deployed to the server.

© 2021 by Rashid Azar. All rights reserved.