Skip to content

Instantly share code, notes, and snippets.

@rahilwazir
Last active April 13, 2019 16:01
Show Gist options
  • Save rahilwazir/ef258c694ff3fc00186b06a4a6b4c8ef to your computer and use it in GitHub Desktop.
Save rahilwazir/ef258c694ff3fc00186b06a4a6b4c8ef to your computer and use it in GitHub Desktop.
Git bare repo for deployments

Create a bare repo

> cd /some/path
> git init --bare myproject
> touch hooks/post-receive
> chmod +x hooks/post-receive
> vi hooks/post-receive

post-receive contents

#!/usr/bin/env bash

TARGET="/home/to/project/root"
GIT_DIR="/path/to/bare/myproject"
BRANCH="master"

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 to production..."
                git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f
        else
                echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
        fi
done

On your local git project

> git remote add production SERVER_USER@SERVER_IP:/path/to/bare/myproject
> git push production master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment