Deploy your project with a git push on your server

Feb 11, 2015 • 2 minutes to read • Last Updated: Apr 9, 2017

As much as you like using github for your public repositories you need a way to host your codebase where you deploy your project sometimes.

You can bookmark this for future reference to get it running quickly. I usually work with ubuntu on AWS EC2 for setting up my projects. To be able to perform these you can use an Linux operating system servers and do it quite easily. So for the purpost of this example I am going to assume you are using EC2 systems too.

Step 1: Login to your EC2 machine through ssh. For ubuntu users login using ssh

ssh ubuntu@[your-server-ip]

Step 2: Once you are in. then you create a git bare repository. You create a directory first and then make it a bare repository. For the purpose of this blog I will call my project unicorn and the bare git repository as unicorn.git.

mkdir unicorn.git
cd unicorn
git init --bare

Step 3 : Create a place you want to export your project files to. Since the default path when I login in EC2 is /home/ubuntu/, I will create a project directory right there and call it unicorn.

mkdir unicorn

Step 4 : Once I am done I need to setup the git hook for whenever my project is updated. Which is called post-update.sample which should be in your unicorn.git folder. We need to first rename the file to post-update.

cd unicorn.git
cd hooks/
mv post-update.sample post-update

Step 5 : Clear everything in there and add this to the file. What it does, is export the git files to the directory /home/ubuntu/unicorn/ . But if you have setup a different path for where you want to export your files then, do so.

#!/bin/sh
GIT_WORK_TREE=/home/ubuntu/unicorn/ git checkout -f

Step 6 : Next logout from the server and goto your local git repository for project unicorn and add a remote.

git remote add production ubuntu@[your-server-ip]:/home/ubuntu/unicorn.git

Step 7 : That’s it ! Now push master branch to the remote production and checkout the /home/ubuntu/unicorn directory for the files.

git push production master

Also if you would like to checkout how to write pre-commit files for __pylint__ing your code then checkout this blog post.