Categories: DevOps

Deploying Git Push Changes To Test/Production Server

On this simple article, you may be able to learn on how to create a git remote repository and easily transfer files or push your updates from local repository to your web hosting server (test or in production).

From Remote Server: Setup Git Repository

1. Connect to the remote server using your root credentials via SSH.

ssh root@192.x.x.x -p22

2. Repository should not be accessible via the web or should create it in the private directory on your account.

cd /var/www/pinoylinux.org

Create a directory for the repository and enter that directory (for testing purposes the generated repository was created under the webroot directory of /var/www/pinoylinux.org ):

mkdir liveserver.git
cd liveserver.git

Note: You can use any desired repository name and it should end with .git.

Create a bare repository and have it initialize:

git --bare init

A bare repository is a repository that is created without a working tree (e.g. actual codes of the project)

3. Then you need to create a post-receive hook, to make sure the push changes or files are sent to the correct location:

vi hooks/post-receive

Add the following to the file and save it:

#!/bin/sh
GIT_WORK_TREE=/var/www/pinoylinux.org git checkout -f

Note: Replace /var/www/pinoylinux.org with correct path of your live web files.

or you may use the following codes below if the above code doesn’t work:

#!/bin/bash

#for debugging
#set -ex

# the work tree, where the checkout/deploy should happen
TARGET="/var/www/pinoylinux.org"

# the location of the .git directory
GIT_DIR="/var/www/pinoylinux.org/liveserver.git"


BRANCH="main"

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

Make sure the file have executable permissions:

chmod +x hooks/post-receive

 

From Local Repository

1. If you do not already have a repository for your website on your computer, you can initiate a new repository and add all files in the current directory with the following commands:

git init
git add .

You should also add your first commit:

git commit -m "My liveserver PinoyLinux"

2. Now you can add a remote to the repository:

git remote add liveserver ssh://root@192.x.x.x/var/www/pinoylinux.org/liveserver.git

Note: The name of the remote is production. You should replace hostname and username: root@192.x.x.x of your server and /var/www/pinoylinux.org/liveserver.git with the location of the git repository.

3. To complete everything, you may now push updates from your local to your web hosting server:

git push liveserver main

 

rockitpinoy

Recent Posts

GitLab CI/CD: Deploy Code to Staging in Minutes

GitLab CI/CD Deployment for Beginners is your step-by-step guide to pushing code to a remote…

11 months ago

Decoding SNMP: From Agents to Managers

Learn SNMP architecture—agents, managers, and MIBs—with real-life scenarios and interesting facts. A beginner-friendly guide.

1 year ago

SNMP 101: A Beginner’s Guide to Network Management

Learn SNMP basics in this beginner-friendly guide. Discover its purpose and how it simplifies network…

1 year ago

Automattic vs WP Engine: What’s at Stake for WordPress Users

When two giants in the WordPress ecosystem go head-to-head in a legal battle, the entire…

1 year ago

Why Choose Linux? An Intro for Newbies

Linux offers freedom, security, and endless customization possibilities. This guide for beginners explains why Linux…

1 year ago

How to install SNMP on Linux Debian 12.xx

Learn how to install and configure SNMP on Linux Debian 12.xx with this step-by-step guide…

2 years ago

This website uses cookies.