QuantumCloud’s Senior Software Engineer Shibly gives a very handy introduction to Version Controlling with GIT
Introduction to Git
We all have experience working on projects that quickly spiral out of control before you know it through gazillions of constantly changing project requirements. It can get pretty frustrating to keep track of all the coding changes and different versions of the code. Experienced programmers know better and they use sophisticated version controlling systems to manage large projects or even small ones that are prone to lot of change requests. If you are new to the version controlling concept, let us introduce you to the basics of it through the best version controlling system ever created in the history of humankind. Behold the king of all version controlling systems – the mighty, the magnificent Git. This will be a series of long tutorials. So brace yourself. This is the 1st installment. Let us get our feet wet.
So, what is Git Actually?
Git is an open source distributed version controlling system – originally developed by Linus Torvalds. Originated from the Linux kernel development, Git is now being used by many popular open-source projects like Android, PHP, WordPress, Drupal, Symfony2, Laravel4 and a lot more. Even many prominent commercial organizations are leveraging the flexibity that Git offers. Git works with the philosophy that “Everything is Reversible” and allows to experiment with new ideas in a project without you needing to worry about breaking anything.
So, Why Should I Use Git as a VCS as Opposed to…?
There are many differences between Git and other VCS(s) like SVN, mercurial etc. But the main difference is, Git is distributed and the others are not. Most VCS(s) require a central source control server to setup and a client application to connect to the server and sync. Git follows a complete different strategy. Git stores the repository on top of the project directory. The repository can be cloned, forked and replaced without any hassle. Besides the repository, Git offers easy branching, nested branching and easy merging. These same features are very difficult for other centralized repository systems. Git offers both local repository and remote repository. Local repositories can be configured as bare or non-bare repository. Bare repositories allow server to share changes coming from different developers whereas non-bare repositories allow developers to create new changes through modification of files and to create new versions in the repository.
Git also allows developers to sync local repositories with the remote repositories. Like users with right privileges can push changes from local repositories to remote repositories or fetch changes from the remote repositories to local repositories. All very useful and extremely convenient.
Features of Git
Git has many exciting features. Here we will discuss only some of the major ones that we use almost every day. One of the very powerful features is branching. Though other VC S(s) have it, Git has some uniqueness that sets it apart. Git offers branch under a branch. So what is actually branching? Branching means, a developer can work on different versions of the same collection of files. The branch actually separates the different versions and allows the user to switch between the preferred branches to work on different versions. Branches in Git are usually local in the repository but can be in remote server as well. Branches can be compared with the other branches and the remote branches.
Another useful feature in Git is branch merging. Git supports that changes from different branches can be combined. The advantage is, while working on a large project, various developers’ works on different branches which can be merged on a future date. Now suppose that a project has two independent branches. 1. Production branch and 2. Development branch. The Dev. branch is used to test and implement new features. This dev branch can later be merged with the production branch for deployment.
Getting Our Hands Dirty
Intrigued yet? Let us learn how to install Git and practically use it on a project. We will use a Microsoft windows operating system for demonstration. On windows, the simplest way to start rolling is to use this URL and install the installer from there. http://msysGit.Github.io/Choose all the default settings in the installation process.
So, you’ve installed Git – the best VCS the human kind has ever created. It’s time to put it into good use. After finishing the install process, you’ll notice that Git comes with its own command line program to work with it. Its name is Git bash. So fire up Git bash but before creating a repository, we’ll do a bit of configuration first. To do those, run the following commands:
git config –global user.name “your name”
git config –global user.email “your email”
We’ll explain later about those configurations. Right now I can say that, those configurations actually hold your signatures. Git offers a wide range of configuration options – from rendering the terminal to color coding the branch tree.
Once configured the Git now knows who the developer is. Let’s create a simple PHP application and initialize the repository there. In your localhost, create a folder named “myPhpApp”. First go to the directory by using the cd command. Like this:
To create a repository we need to run the git init
command. As you have guessed, git init
actually initiates a Git repository inside the project folder and adds a .git folder inside it. You can consider the .git folder as a code history book. Every single history is recorded there for eternity.
So, you’ve successfully created your first Git repository. Congrats!
That’s a lot for today. Stay tuned for the next installment. On the next part, we’ll be discussing on adding files on the repository, committing and updating files to the repository and more on the idea of branching. A little sneak peek. By running the git init
command, Git actually creates a master branch in the repository. Check the second screenshot. Phew! Thank you for reading and keep practicing. You can read the official documentation on Git from here http://Git-scm.com/book/en/Getting-Started-About-Version-Control
Really nice article. Thanks for giving a nice article Mr. Shibly.