I recently needed to automatically deploy my site from git (bitbucket) to a server whenever someone pushed to the master branch of the repository. This is a quick and easy PHP script I came up with to solve the problem.

<?php
    // the location of the repository
    $git_dir = '/var/www/html/site';
    // The commands
    $commands = array(
            'echo $PWD',
            'whoami',
            'git reset --hard HEAD',
            'git pull',
            'git status',
            'git submodule sync',
            'git submodule update --remote --merge',
            'git submodule status',
    );
    chdir($git_dir);
    // Run the commands for output
    $output = '';
    foreach($commands AS $command){
            // Run it
            $tmp = shell_exec($command.' 2>&1');
            // Output
            $output .= "<span style=\"color: #6BE234;\">\$</span> <span style=\"color: #729FCF;\">{$command}\n</span>";
            $output .= htmlentities(trim($tmp)) . "\n";
    }
?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>git deployment</title>
</head>
<body style="background-color: #000000; color: #FFFFFF; font-weight: bold; padding: 0 10px;">
    <pre>
        <?php echo $output; ?>
    </pre>
</body>
</html>

Place a PHP file containing the code above in a place where you can serve it, then navigate to it from your browser to test it out.

Whenever the page is called, it will cd into the directory at the top, reset all local changes, pull, and update submodules if there are any.

You can then point bitbucket or github to the URL of the PHP script to be executed automatically when a push to the repository occurs.