Installing Git and Setting Up Automatic Deployment on Ubuntu

Git installation and auto deployment on ubuntu

Introduction

By far, the most widely used modern version control system in the world today is Git. Git is a mature, actively maintained open source project originally developed in 2005 by Linus Torvalds, the famous creator of the Linux operating system kernel. A staggering number of software projects rely on Git for version control, including commercial projects as well as open source. Developers who have worked with Git are well represented in the pool of available software development talent and it works well on a wide range of operating systems and IDEs (Integrated Development Environments).

The raw performance characteristics of Git are very strong when compared to many alternatives. Committing new changes, branching, merging and comparing past versions are all optimized for performance. The algorithms implemented inside Git take advantage of deep knowledge about common attributes of real source code file trees, how they are usually modified over time and what the access patterns are.


Install Git with Apt

By far the easiest way to install Git is by apt, though the version you might get may not be the latest it may be a faster alternative for the other sytle of installation if you want to set-up Git as quickly as possible.

You will just have to update your package and install Git afterwards.

sudo apt-get update
audo apt-get install git

Install Git from Source

Installing Git from source requires downloading the latest source code of Git then compiling to create an installer. Before you begin, you need to install the software that git depends on. This is all available in the default repositories, so we can update our local package index and then install the packages:

sudo apt-get update
sudo apt-get install build-essential libssl-dev libcurl4-gnutls-dev libexpat1-dev gettext unzip

Afterwards you can download the latest Git version you want going to the git project's page on Github[1].

Click on Branch: master then go to tags and click on the version you want to install.

Git Download Page

On the next page click on Clone or download then right click on Download ZIP and select Copy Link Address.

Git Download Page 2

Lets go back to your Ubuntu Server. Download the file using wget.

wget https://github.com/git/git/archive/v2.9.2.zip

Unzip the downloaded file and move inside the directory.

unzip v2.9.2.zip
cd git-2.9.2

Make the package and install by by the commands.

sudo make prefix=/usr/local all
sudo make prefix=/usr/local install

Now that Git is installed we can now configure it for our use.


Setting up Git

We will still need to make some changes so our commit messages will contain our information. We can do it through the command git config.

We will need to provide a name and an email address, we can do this by running the commands below:

git config --global user.name "Your Name"
git config --global user.email "youremail@domain.com"

We can see our configuration by typing.

git config --list

The corresponding result will be like:

user@gitrepo:~# git config --list
user.name=Your Name
user.email=youremail@domain.com

Setup Automatic Deployment with Git

Our Server Setup

Our workspace:
Your server live directory: /var/www/html/mydomain.com
Your server repository: /var/repo/mysite.git
What should we do if we want to push to mysite.git and at the same time make all the content available at /var/www/html/mydomain.com?

Creating Repository

type the following from the command line:

cd /var
mkdir repo && cd repo
mkdir site.git && cd site.git
git init --bare

---bare means no source files, just version control.

Hooks

Git documentation define three possible server hooks: 'pre-receive', 'post-receive' and 'update'. 'Pre-receive' is executed as soon as the server receives a 'push', 'update' is similar but it executes once for each branch, and 'post-receive' is executed when a 'push' is completely finished and it's the one we are interested in.

In our Git repository we type:

ls

You can see a list of folders after executing the command.

user@gitrepo:/var/repo/mysite.git# ls
branches  config  description  HEAD  hooks  info  objects  refs

We will go to folder hooks.

cd hooks

Now, create the file 'post-receive' by typing:

cat > post-receive

When you execute this command, you will have a blank line indicating that everything you type will be saved to this file. So let's type:

#!/bin/sh
git --work-tree=/var/www/html/mydomain.com --git-dir=/var/repo/mysite.git checkout -f  

When youa re finished typing press Control + D to save. To execute the file we set the proper permission and make it as an executable.

chmod +x post-receive

Local Machine

Now we will create our local repository, type the command below to exit.

exit

and create your repository:

cd /my/myworkspace
mkdir myproject && cd myproject
git init

To configure the remote path of our repository we type.

git remote add live ssh://user@mydomain.com/var/repo/mysite.git

To add a ready work to our Git repo we do.

git add.
git commit -m "This project"

Going Live From the Server

When its time to go live from beta you push it on the server.

cd /var/repo/beta.git
git push live master

Thats it! Your server is now set to automatically deploy!

[1]: https://github.com/git/git