In this tutorial, we will learn how to setup and push code from local machine to live server using Git.

I assume that you have basic knowledge of Git and Linux ubuntu. Git is used for version control and we can keep track of each task and team can sync up with each other’s work.

First, create your repo on a local machine.

cd /var/www/html (ubuntu server has this path)

mkdir project

cd project

git init OR git clone URL of your project

git checkout your branch

We are done with setting up project using git on the local machine. Now we will setup server to listen whenever we push anything from the local machine.

Create Repository on server

cd /var

mkdir repo

cd repo

mkdir site.git

cd site.git

git init --bare

–bare indicate no source files it will have only version control.

Now we will create a hook to receive the update from the local machine. In the git, all the repositories have default folder ‘hooks’ and populated with some pre-default sample hooks. We can create custom hook e.g. pre-receive, post-receive hook to perform actions.

You can type the following command to view all files and folder and create a post-receive hook.

ls

cd hooks

cat > post-receive

When we type above command it will give blank line where we can type following command.

#!/bin/sh

git --work-tree=/var/www/html/project-folder --git-dir=/var/repo/site.git checkout branchname -f

Press ‘control+d’ to save above command, Now change the permission for post-receive using the following command.

chmod +x post-receive

Now again back to the local machine. (assuming you are in your project folder e.g. /var/www/HTML/project-folder)

Run the following command

git remote add live ssh://user@domain or IP address/var/repo/site.git

Here we have just added remote URL that points to our live server. You can now edit your files and add it with the commit message.

git add .

git commit -m “Commit message”

We used ‘.’ dot with add command that will add all files. Now, we can push a change to our live server using following command. Here we used ‘live’ as we previously added it using ‘git remote add live’ command.

git push live branchname

If you get successful message, then our deployment is successful 🙂

Want to work with us? We're hiring!