Thursday, November 22, 2012

GIT Basics

Version Control Systems

As software developers most of the time we are experienced collaborative development where many guys are working on a single code base. To make this collaborative work efficient the concept called Version Control System was introduced. VSC can be divided in to two main categories.
  • Centralized Version Control Systems (Subversion)
  • Distributed Version Controller Systems (Git, Mercurial)
Centralized Version Control Systems


 Above figure[0] describes how the Centralized Version Control System works. Central server consists of all the version data  and logs. Client computers can checkout a specific version from the central server and the copy only contains the file itself. So the Central server is the only place where you keep version controller files. What happens when the central server is being stopped for maintenance purpose or similar issue ?  You cannot do a single version control operation including commits, taking diffs with server branch , etc. Simply you cannot continue you work collaboratively in offline mode.Even more worst ,think of a scenario where you do not take backups frequently and the server crashed. So you lost entire version history.


To address those drawbacks software industry came up with the concept of Distributed Version Control System.
Distributed Version Controller Systems 


In distributed Version Controller System it has a server computer and all the clients can clone the entire repository into local machine. So that each client repository is a candidate for backups. And also we can do operations on the repository locally and later or synchronize the work with the server repository.

Working with Git
First you need to download git client and install it on your local machine.[2]. It provides you command line client as well as a GUI client. For learning purpose I will use command line client for this post. As for the code hosting server I am going to use git hub and will create a repository called "HelloWorld" and following is the url that repository.
    https://github.com/esudharaka/HelloWorld.git

-Clone a remote repository




- Create HelloWorls.java using internal Vi editor



 - Check the status or current branch (in this case master)
 You can see HelloWorld.java file is un-tracked and we need to add it.
- Add HelloWorld.java using git add command and check the status

 










Now you can see git  says that you have a new file and  suggest the commit command. Before committing the changes locally lets take a look how the git diff command works.

 













In this stage we have not commit HelloWorld.java file yet. But we have added it to the stage state where we can commit the changes in next step. So when we execute git diff command it will show no difference. Because it does consider only the committed files. But if we use --cached argument with the diff command it will show the changes in the staged file. So we can see the content of HelloWorld.java class as the difference.

-Commit HelloWorld.java







After the commit step it mean you have committed the file in to your local repository. Not to the one stay in Git hub.(We will do it latter) 

- Create a branch 

Following are the steps of creating a branch called "feature" and switch to that branch.















Then I will add a new file Feature.java and modify HelloWorld.java and commit them into feature branch. Now Branch looks like this.


Suppose that we finished implementing new feature and we need to merge it with master.
- Merging a branch 


















First we need to switch to master. Then by using git merge command you can merge master with feature branch. In this case luckily we don't have any conflicts. 

For the demonstration purpose I made conflicts in master and feature branch. Now when I run git merge command it says that you have conflicts.





Then you need to use git mergtools command and it will shows available merge tools available in your local machine.


For this purpose i am going to use tortoisemerge tool. Onece you press enter button git will open merge tool for you as bellow.




 Once you resolve conflict you can close this merge tool. Then we will check the status of master branch.









Then you need to do your final commit in to you master branch.







So far we played with our local repository. And now its time to push you changes into remote repository on the github.








There are some more advanced topics like rebase, interactive rebase, making pull requests. References


[0] - http://git-scm.com/book/en/Getting-Started-About-Version-Control
[2] - http://code.google.com/p/msysgit/