In a previous post, I wrote an article about GIT repositories and included various commands and helpful information on how to use Bitbucket and GIT. I’ve been learning how to use GIT since we started using it for Advertise Me.

Managing and updating your GIT repository can be challenging if you are not very technical or if you are not familiar with using GIT. In this post, I’m going to share with you how to use PHP to pull changes from your GIT repository and in particular how to create a PHP script to do this. This is particularly useful when your team doesn’t want to learn or have any idea about GIT. The script I will be sharing with you requires certain security permissions so make sure you test the security requirements before implementation. If security is a concern, then you should run the GIT commands manually and not use a PHP file script at all. The PHP file will pull changes from your Bitbucket repository branch (in my case it will be from the development or from the master) depending on the location of the file.

One issue I did receive was a fatal error when pulling a change from Bitbucket. With this particular issue, it was related to permissions and I needed to remove the SSH key from the repo settings and add it to my personal profile setting. This video HOW TO FIX ISSUE WITH BITBUCKET FATAL ERROR – GIT PUSH will help you fix this issue

Here’s a script I created to pull from Bitbucket. You already need to have your SSH keys set up and GIT configured for this script to work.

The Simple Entrepreneur HOW TO RUN GIT PULL IN PHP script
<?php

echo "GIT PULL FROM DEVELOPMENT BRANCH";
echo "<br>";
// Checks start
if(function_exists('exec')) {
    echo "exec is enabled";
}

echo "<br>";
echo exec('whoami');
echo "<br>";
echo exec('which git');
echo "<br>";
// Checks end

$repositoryPath = '/path';

// Set the branch you want to pull from
$branch = 'development';

$result = exec("cd {$repositoryPath} && sudo git pull origin {$branch} 2>&1", $r2);

echo "<pre>";

foreach ($r2 as $line) {
        echo $line . "\n";
}

unset($r2);

echo "\n\n";
echo "------------------------------------------------------";
echo "\ngit status\n";
echo "------------------------------------------------------";
echo "\n\n";

$result = exec("git status 2>&1", $r2);

echo "<pre>";

foreach ($r2 as $line) {
        echo $line . "\n";
}

You can download the script here, just make sure you rename the file from gitpull.txt to gitpull.php:

The function below is used to check whether exec is enabled and if it is, you can remove it.

if(function_exists('exec')) {
    echo "exec is enabled";
}
echo "<br>";
echo exec('whoami');
echo "<br>";
echo exec('which git');
echo "<br>";

I used command whoami to check which account the script was using so that I can give it the right permissions.

I then used the which git to check the location of the git command.

Once you run the script you will receive something similar to this:

The Simple Entrepreneur HOW TO RUN GIT PULL IN PHP output

If you receive errors when running the script such as

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

sudo: no tty present and no askpass program specified

Then it means the admin web account does not have the required permissions to run the git command. To solve this problem, the visudo command which will allow you to add the permission for the admin account and the git command. Just add the following line

admin ALL = NOPASSWD: /usr/bin/git
image 1

If you execute the PHP file and don’t see the results of the git pull it may mean that exec is not allowed. To allow exec you need to remove exec from the php.ini file under disable_functions =


disable_functions = pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,system,passthru,shell_exec,proc_open,popen

I hope this helped and if it does, don’t forget to subscribe to the newsletter.

Subscribe to my newsletter where I will share my journey in affiliate marketing, business, technology, fitness and life in general. Hopefully, this motivates you to also change your journey in life.

This field is required.

Subscribe to my newsletter where I will share my journey in affiliate marketing, business, technology, fitness and life in general. Hopefully, this motivates you to also change your journey in life.

This field is required.

If this article helped you in any way and you want to show your appreciation, I am more than happy to receive donations through PayPal. This will help me maintain and improve this website so I can help more people out there. Thank you for your help.

HELP OTHERS AND SHARE THIS ARTICLE


799Shares

LEAVE A COMMENT