Mar 11, 2016

Why and How to begin with Git!

It's been hectic past few days and I didn't get a chance to write any blog. Busy with my project work, I got very little time for anything else.

Today, let's check out Git and how it can be useful for us.

I have split this blog into the following sections:-
  1. What is Git?
  2. Why should we use Git?
  3. Getting started with Git.

What is Git?


As the first sentence on the homepage of Git says, it is a free and open source distributed version control system.

Let's look at what this line means.

Free and Open Source - Git uses the GNU General Public License version 2.0. This license ensures a lot of things, few of which are:-
i) The source code should be present.
ii) Anyone who possesses this software can redistribute it without having to pay any fees or royalty.

Distributed Version Control System - For most of us who have worked on MS stack, TFS is one of the most popular version control systems. 
TFS is an example of a Centralised Version Control System(CVCS). What this means is that we have one server which maintains the code base and all the users connect to that server for any operation. 
In a Distributed Version Control System(DVCS), we have one repository from which all the users clone the code onto their own systems. Due to this, the user does not need to contact the server for each operation and lot of operations can be done locally. Also, this creates multiple copies of the code and in the event of any crash of the central repository, we can easily restore the data from any of the user.

If you see the URL of the link to Git above, it says http://git-scm.com.
What is scm? SCM stands for Software Configuration Management. This is a term used to describe the process of tracking and controlling changes in the software.


Why should we use Git?


Git offers a lot of benefits to its users:-  
  1. Almost all the operations in Git are performed locally. Due to this, any operation in Git appears lightening fast to someone like me, coming from TFS background(CVCS).
  2. Another reason for Git being so fast is that it is written in C. This removes the overhead of runtime associated with higher-level languages.
  3. The best feature of Git is the way it handles branching and merging. Git actually encourages users to create multiple local branches and since any such operation takes just seconds, it's very convenient. So, you can create a branch for any new thing you want to try or a bug you want to fix or anything like that. 
  4. For every file or commit that we make to Git, a checksum is calculated and associated with it. This ensures the integrity of the file. It ensures that you get only what you have put in the file.
  5. Git offers a concept of Staging area. This acts as an intermediate area where we can consolidate our changes before actually committing them. We can always choose to override this option, if we want.
  6. When you clone a copy of the repository on your local system, you are getting the entire history of the changes made in the repository. You can compare and check the history of any file even when you are not connected to the internet. That is a really cool feature. 
  7. The open-source community on Github(code-hosting platform for version control and collaboration) is very active. There are so many amazing open-source projects hosted on Github. Following are few examples but you can check many more interesting ones. 
  • Linux - Git started off as a version control system for Linux. The code here is in C but do go through bits of it if you get a chance. 
  • Roslyn - This is the .NET Compiler platform for C# and Visual Basic. 
  • .NET Core - It is subset of the .NET Framework which was created so that it can be open-source and be cross-platform. 
  • EF Core - It is the ORM provided by Microsoft which can be used on top of .NET Core to build cross platform applications. Though it is still in prerelease and will take time to mature, we can always go and check out the code. 
  • AutoMapper - This is the object mapper which is frequently used in .NET projects. 


Getting started with Git


We will see the following basic operations that we can perform on Github.
  1. Create a Repository
  2. Create a Branch
  3. Make changes to a file and push them as commits
  4. Open and merge a pull request

Before all this, we need to have a Github account. So, sign up for free and get a Github account.
For simulating two different users, I have created two Github accounts(roshan-kumar001 and rks-hs001).
Following is the Github homepage when we log in.


1) Create a Repository - We can create a new repository as shown below.


Here, we select Public repository for a free account. If you have a paid account, you can go for a Private repository as well. As the name suggests, anything you store in a Public repository can be accessed by anyone else. We select the checkbox to initialize the repository with a README.md file.


2) Create a Branch - Creating a branch is as simple as shown below.


3) Make changes to the file and push them as commits.
Here, I would just demonstrate changes to the README.md file which is already added to the project but you can have the whole Visual Studio solution and the same concept would apply(Though it does involve adding of file and extension types to .gitignore, I will take it up in a separate blog maybe).
We can make changes to the README.md file using the Edit link as shown below.

Add a new line to this file as shown below and commit the changes.




4) Open and merge a pull request.
Now, I log in with another account and search for the new repository I just created. Go to the develop branch, as shown below.

Edit the README.md file and add a line of text to it.

On updating the changes, it asks for us to compare the files and to create a Pull Request which sends a notification to the base repository owner.

On my original repository, we get a notification informing about the Pull Request as shown below.

Since this file does not have a conflict, we can directly merge it. Else, we'll get merge conflict option where we can merge the two files.




This is the basic workflow of how a lot of open-source projects on Github work. So, as soon as you understand this, you can check them out and even start contributing to it. Go out and explore Github and the amazing projects that are present on it. I'm sure it'll help all of us in becoming better developers.

I hope this article provides the basics of Git and helps you to get started on Github.


No comments:

Post a Comment