How to Deploy a Project to a Remote Server / Production Server Using Git

Nahid Hasan Limon
4 min readJul 2, 2021

In the beginning, maximum developers think deploying a project in the remote server is too complex. They don’t even try for a long time but by doing a couple of steps we can save time and can maximize our productivity.

Step 1 :

At first, open any CMD tool (prefer. Git Bash ). log into your remote server.

by default, the username should be root or write down your username and IP address and don’t forget to use SSH protocol.

ssh <your_user_name>@<your_ip_address>

After run this command, put your password and press enter. If your credentials is ok then you will see a screen like this attached picture.

Login to our remote server using git bash

Step 2 :

Our target is directly deploying our local source code into a remote server using git. We know, pushing code to a remote branch hosted by GitHub itself is a matter of a minute. in this scenario, we directly push our code into our remote server. so that we have to use an intermediary repo that will ease our thing. we called it a bare repository.

If you haven’t installed git in your remote server you can install it by

sudo apt-get install git

then you can do anything like your local machine. you can pull, clone, fetch , push etc .

Step 3:

Create a folder that will act as an intermediary repo. for this article, we can name this intermediary.git. here , ‘intermediary’ is just a word . you can name anything. ant .git extension used only for understanding purpose. if you want you can name this directory without git extension.

mkdir intermediary.git

After that we have to initialize a git repository in our remote server using the git init command but i’ve already mentioned it have to be a bare repository so we will run

mkdir intermediary.git
cd intermediary.git
git init --bare

Step 4 :

Create a directory/folder for deployment of your website in your remote server by using git bash . If you are using Apache then your production directory looks like /var/www/ for this tutorial we assume our folder name will be testautodeploy.

cd /var/wwwmkdir testautodeploy

testautodeploy is just a name of a folder. you can give any name. Remember testautodeploy full path is /var/www/testautodeploy . If you are using NginX then it could be different path. (/var/www or /var/www/html is the default path for apache). so you just focus on new folder.

So Now , we have two new folder . one is testautodeploy which is deployment folder in the directory of /var/www . another is intermediary.git that will act as middle man. and we initialized a bare repository in this folder.

Step 5:

now we push from our local repo it will directly push into our remote server’s testautodeploy folder by the help of intermediary.git . but we didn’t set any connection between them . now it’s time to set a connection . this connection will be made by using git hooks .

From local machine, we won’t push directly into our testautodeploy . Actually, we will push into our repository which is in intermediary.git folder and after push it will call a post-receive hook. This will take care the transfer between intermediary.git & local-testautodeploy folder.

cd ~
cd intermediary.git
cd hooks

Now we are in the hooks directory . now we have to edit/create post-receive hooks by using any editor. for this article we’ll use nano editor.

nano post-receive

Now change the contents of this hook.

#!/bin/sh
git --work-tree=path_to_deployment_folder --git-dir=path_to_git_directory checkout -f name_of_branch

then save post-receive hook CTRL+O then exit with CTRL + X. Well done!

Here ,

name_of_repository can be any name you want. In this article testautodeploy

replace path_to_git_directory with the path to the bare git repository, which in this case is /var/www/testautodeploy

now its time to tell our server that give post-receive hook’s desired permission by another command.

chmod +x post-receive

Step 6:

Well Done! Now we are set to push from our local repo.

Open your local project folder .(Your local machine project folder)

git initadd .git commit -m “commit message”git remote add name_of_repository ssh://your_user@server_ip_address/path_to_git_directory

for this article it could be git remote add name_of_repository ssh://<user>@<your_remote_server_ip_address>/~/testautodeployonproduction.git

Now, the final command.

git push name_of_repository master

here, master is the branch. you can push any branch.

Congratulations!

If you face any problem please make sure the path of these projects are correct. In the beginning , we all survive with the file structure of linux system.

--

--