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.
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
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.
Click on Branch: master then go to tags and click on the version you want to install.
On the next page click on Clone or download then right click on Download ZIP and select Copy Link Address.
Lets go back to your Ubuntu Server. Download the file using wget.
Unzip the downloaded file and move inside the directory.
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.
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 "email@example.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 firstname.lastname@example.org
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?
type the following from the command line:
mkdir repo && cd repo
mkdir site.git && cd site.git
git init --bare
---bare means no source files, just version control.
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:
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.
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
Now we will create our local repository, type the command below to exit.
and create your repository:
mkdir myproject && cd myproject
To configure the remote path of our repository we type.
git remote add live ssh://email@example.com/var/repo/mysite.git
To add a ready work to our Git repo we do.
git commit -m "This project"
When its time to go live from beta you push it on the server.
git push live master
Thats it! Your server is now set to automatically deploy!