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 [email protected] -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: [email protected] 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
rockitpinoy
Articles: 10

Leave a Reply

Your email address will not be published. Required fields are marked *

Protected by CleanTalk Anti-Spam