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.

    // the location of the repository
    $git_dir = '/var/www/html/site';
    // The commands
    $commands = array(
            'echo $PWD',
            'git reset --hard HEAD',
            'git pull',
            'git status',
            'git submodule sync',
            'git submodule update --remote --merge',
            'git submodule status',
    // 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";
<html lang="en-US">
    <meta charset="UTF-8">
    <title>git deployment</title>
<body style="background-color: #000000; color: #FFFFFF; font-weight: bold; padding: 0 10px;">
        <?php echo $output; ?>

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.