Skip to content

Instantly share code, notes, and snippets.

@tajidyakub
Last active December 1, 2017 10:12
Show Gist options
  • Save tajidyakub/48d6f2f997c34b013e76ad34912c7f64 to your computer and use it in GitHub Desktop.
Save tajidyakub/48d6f2f997c34b013e76ad34912c7f64 to your computer and use it in GitHub Desktop.
Creating a bare remote git repository for project deployment with git push

Bare Remote git Repository for deployment

Remote Server Prep

Prepare the working directory and directory for the git repo. Example is using root login with ssh keybased auth, configured in ssh server.

# mkdir -p /root/repo/projectname.git
# mkdir -p /var/www/projectname/public_html

Initialize bare git repo in the git repo dir and setup post-receive hook

# cd /root/repo/projectname.git
# git init --bare
# vim hooks/post-receive
# chmod +x hooks/post-receive

post-receive is an executable file containing bash script as follows;

Content of hooks/post-receive

#!/bin/sh
git --work-tree=/var/www/projectname/public_html --git-dir=/root/repo/projectname.git/ checkout -f
chown -R www-data.www-data /var/www/projectname/public_html

Notice the additional chown command so the ownership of the pushed data changed to www-data user, not recommended for a large project, need to find another method.

Development Machine prep

In the local machine ssh is configured to login to remote server via ~/.ssh/config file as follows:

Content of ~/.ssh/congfig file

Host project
        Hostname server.ip.address
        Port 22
        User root
        IdentityFile ~/.ssh/keyfilepath

So we can login to remote server using ssh only with an alias such as ssh project;

Create the directory for the local project development, init local git repo and add remote git repo

$ mkdir ~/Project/www-deploy/ && ~/Project/www-deploy
$ git init .
$ git remote add remote ssh://project/root/repo/project.git
$ touch .gitignore
$ git add .
$ git commit -m "Initial commit"
$ git push remote master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment