Automatically pulls a git repository when commits are pushed.

2.1.2 2015-12-11 23:26 UTC

This package is auto-updated.

Last update: 2020-08-28 00:58:52 UTC


Automatically pull when changes are pushed to a Git repository. (Actually it does a git fetch followed by git reset but that wasn't as catchy.)


There are two important parts:

  • A PHP script which Bitbucket or GitHub will automatically send a request to when you push. (http://mysite/deploy.php in the examples below)
  • A shell script which does the actual pulling. (scripts/

The reason for the separation is so you don't need to grant the web user write permission to your files. You just need to allow it to run the one script as a user that does have write permission.


  • Install the latest version with composer require tmd/auto-git-pull

  • Make the pull script executable (you need to do this if you update the package as well):

chmod +x vendor/tmd/auto-git-pull/scripts/

You can have this automatically happen by adding this to your composer.json:

"scripts": {
    "post-install-cmd": [
        "chmod +x vendor/tmd/auto-git-pull/scripts/"
  • Create a publicly accessible URL on your site which will be called by GitHub/Bitbucket and run the deployment (e.g. and set the parameters as appropriate.

Example showing all the options that can be given and their default values: The only required option is directory

use Tmd\AutoGitPull\Deployer;

require 'vendor/autoload.php';

$deployer = new Deployer([
    // IP addresses that are allowed to trigger the pull
    // (CLI is always allowed)
    'allowedIpRanges' => [
        '', // Bitbucket
        '', // Bitbucket
        '', // Bitbucket
        '', // Bitbucket (Dec 2015)
        '', // Bitbucket (Dec 2015)
        '', // GitHub

    // These are added to the allowedIpRanges array
    // to avoid having to define the Bitbucket/GitHub IPs in your own code
    'additionalAllowedIpRanges' => [

    // Git branch to reset to
    'branch' => 'master',

    // User to run the script as
    'deployUser' => 'anthony',

    // Directory of the repository
    'directory' => '/var/www/mysite/',

    // Path to the pull script
    // (You can provide your own script instead)
    'pullScriptPath' => __DIR__ . '/scripts/',

    // Git remote to fetch from
    'remote' => 'origin'

$deployer->postDeployCallback = function () {
    echo 'Yay!';


Example in Laravel showing minimal options:

Route::post('deploy', function()
    $deployer = new \Tmd\AutoGitPull\Deployer(array(
        'directory' => '/var/www/mysite/'

Example with logging:

use Monolog\Logger;
use Monolog\Handler\FingersCrossedHandler;
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Handler\NativeMailerHandler;
use Monolog\Handler\StreamHandler;
use Tmd\AutoGitPull\Deployer;

require 'vendor/autoload.php';

$deployer = new Deployer([
    'directory' => '/var/www/mysite/'

$logger = new Logger('deployment');

// Output log messages to screen
    new StreamHandler("php://output")

// Write all log messages to a log file
    new RotatingFileHandler('/var/log/mysite-deploy.log')

// Send an email if there's an error
    new FingersCrossedHandler(
        new NativeMailerHandler('', 'Deployment Failed', 'anthony@localhost', Logger::DEBUG),
        new ErrorLevelActivationStrategy(Logger::ERROR)


  • Add the hook on Bitbucket/GitHub to run the script:

Add bitbucket deploy hook

If the web server user does not have write permissions on the directory

If your webserver runs as a different user than the owner of the files (as is best practise) you need to allow the webserver to do the pull.

  • Allow the web server user to run the pull script as a user with write permissions:
sudo visudo

# Add the line:
# (Edit users and path as appropriate)
# www-data = User the PHP script runs as
# anthony = User the shell script needs to run as to write to the directory
# /var/www/mysite/vendor/tmd/auto-git-pull/scripts/ = Path to shell script

www-data ALL=(anthony) NOPASSWD: /var/www/mysite/vendor/tmd/auto-git-pull/scripts/
  • Set the user to run the pull as in the parameters:
$deployer = new \Tmd\AutoGitPull\Deployer(array(
    'deployUser' => 'anthony',
    // ...

If your repository is private

You need to setup a deployment key so the pull can happen without a password being entered.

  • Generate an sshkey using ssh-keygen *for the user that will run the pull script (e.g. www-data).

  • Follow these instructions to add a deployment key to the git repository:

Bitbucket: GitHub:

  • Change your git remote url from HTTPS to SSH if necessary:
cd /var/www/mysite
git remote -v

If your output looks like this, you're using HTTPS:

origin (fetch)
origin (push)

Change it to use ssh, like this:

git remote set-url origin