What is Git?

A warm welcome everyone, it’s your friend gutsytechster!!

Today, we are going to learn about Git. Tell me if you have ever heard this word or if it seems familiar to you. If it is a yes, then my friend you are going to enjoy the session. However, if it’s a no, well you have come to the right place my fellow, it’s for you. Before Git, we need to understand the concept of version control.

Version Control

Now suppose, you are working on a project with your team members. In my experience, a project cannot be done in a single day, especially when it’s a team project. Everyone puts his/her efforts to improve the project which takes time to decide which is appropriate according to it. It takes days, months or in many cases years, depending upon the project. Now if you want to keep track of the changes which have been done by your team members, who has done the changes? When has the changes been done? Why the change needed? In simple words you want to keep track the version control of your project. Hence, Version control means to maintain the history of each version(change) of your project.

Version Control is done by the Version Control Systems(VCS). There are two types of VCS:

  • Centralized Version Control System
  • Distributed/Decentralized Version Control System

Centralized VCS maintains a central server in which all the files and folders related to project are stored. The main drawback of Centralized VCS’s is their dependency on central server. Now suppose, if for an hour the server goes down, anyone cannot contribute during that hour or if the disk got corrupted and proper backup was not taken then all the data will be lost. To overcome this drawback Distributed VCS’s are developed which do not depend on server, rather it mirrors the actual repository. If server goes down, then the repository can be copied from any team member to restore the server.

To your curiousness, let me tell you Git is a Distributed Version Control System.


There are some basic terminology which we need to understand:

  1. Repository: It is the folder which encompasses the file and sub-folder associated with project along with each file’s version history.
  2. Blob: Blob stands for Binary large Objects. Each version of a file is represented as a binary file which is named as SHA1 hash of that file.
  3. Commit: It is an action implies that you want to execute the changes permanently in the repository with a suitable commit message.
  4. Staging Area: In Centralized VCS’s, the modifications are directly commited to the repository, while the git looks for the changes present in staging area, only those changes can be commited to the repository. So, if there is any modification in any file or folder, firstly that has to be added to staging area and then it will be commited. It is also known as index.
  5. Forking: Forking means to copy someone else’s repository to your account so that you can contribute to it.
  6. Pull: Pull means to copy the changes from a remote repository to your local one so as to update your revision history.
  7. Push: Push means to send your modifications to the remote repository.
  8. Pull Request: It is the request you send to the remote repository to accept your changes.
  9. Cloning: This means to copy one of your repository to your local machine so that you can work on it.
  10. Branches: Branches are used to create another line of development. By default, Git has master branch as default, however you can create another branch to work on a specific feature. Once the feature is ready, it can be merged with master branch.
  11. Merge: It is an operation to combine two branches.
  12. Merging conflict: The conflict arises when two branches have different modifications on the same element because of which git can’t decide which one has to be merged.
  13. HEAD: HEAD is a pointer which always points to the latest commit in a branch.

Git Commands

Now, we will be using Git commands for version control. Git can does version control using GUI and terminal both. Here I will use my linux terminal(Ubuntu) for it. So let’s begin by installing Git. For installation we can use sudo command as:

~$ sudo apt-get install git

It will prompt you to enter password. After entering password your installation process will begin. You can check if Git has successfully installed by looking for the man page of it as:

~$ man git

It will open the manual page for Git. Every Git command starts with the keyword “git”.

Very first thing we need to do is to configure our Git profile with a username and email.

~$ git config --global user.name "gutsytechster" 
~$ git config --global user.email "gutsytechster@example.com"

You can check by using the command:

~$ git config --list

It will show your username and your email. Now, create a directory in your home/Public/ as :

~/Public$ mkdir GitLearning

You can give any name to your directory. Now, change the directory to GitLearning

~/Public$ cd GitLearning

Now, this directory will be your repository. After changing the directory use the command:

~/Public/GitLearning$ git status
fatal: Not a git repository (or any of the parent directories): .git

git status tells the current status of the repository. Here it is showing that, it is not a Git repository. So, this means we have to make it a Git repository which will be done by:

~/Public/GitLearning$ git init
Initialized empty Git repository in /home/raj/Public/GitLearning/.git/

git init is used to initialize a local repository to Git repository. Now Git can track the changes in files and folders in it. Now again check the git status.

~/Public/GitLearning$ git status
# On branch master
# Initial commit
nothing to commit (create/copy files and use "git add" to track)

Our first commit i.e. initial commit was to initialize an empty repository. Let’s create a file now:

~/Public/GitLearning$ nano HelloWorld

This will open the text editor and then we write a few lines, save and exit(Ctrl+x)

This is the first file in this repo.
And We are learning Git.

Now again see git status:

~/Public/GitLearning$ git status
# On branch master
# Initial commit
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
# HelloWorld
nothing added to commit but untracked files present (use "git add" to track)

It shows that there is a file which can be committed, but as we know for committing a file, first we have to put the file into staging area. This can be done by:

~/Public/GitLearning$ git add HelloWorld

git add is used to put the file into staging area. To put all the files present in repository into staging area we can use git add -A. Now again look at the git status:

~/Public/GitLearning$ git status
# On branch master
# Initial commit
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
# new file: HelloWorld

Now, it is clearly shown that, Git is keeping track of every action which is been done in the repository. Now the file can be committed. To commit the file we will use:

~/Public/GitLearning$ git commit -m "Created the file"
[master (root-commit) eb36bd3] Created the file
1 file changed, 2 insertions(+)
create mode 100644 HelloWorld

git commit command is used to commit the file and it takes the commit message(after “-m”) to describe the action, without which it won’t allow you to commit.

You can also unstage a file from the staging area by using git rm –cached <filename>. Now again look at the git status:

~/Public/GitLearning$ git status
# On branch master
nothing to commit, working directory clean

Now, the file has been committed to repository and there is nothing to be committed. Now let’s do some changes:

~/Public/GitLearning$ nano HelloWorld

Then add a line:

This is the first file in this repo.
We are learning the Git.
It is a Version Control System.

Let’s see the git status:

~/Public/GitLearning$ git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
# modified: HelloWorld
no changes added to commit (use "git add" and/or "git commit -a")

As we can see, Git told us about the modification of the file. Since, we did modification in the file, so we again have to put it into staging area to commit and then we can commit it:

~/Public/GitLearning$ git add HelloWorld
~/Public/GitLearning$ git commit -m "Added a line"
[master 77f6160] Added a line
 1 file changed, 1 insertion(+)

Now to see, how many commits have been done in the repository, we use the command git log, which shows all the information related to Git

~/Public/GitLearning$ git log
commit 77f6160313e4c72b95d6a3291eefda1be12f915e
Author: gutsytechster <gutsytechster@example.com>
Date: Tue Jan 2 22:59:46 2018 +0530

Added a line

commit eb36bd388f7cc1a074e11516a8034c86fd7f551d
Author: gutsytechster <gutsytechster@example.com>
Date: Tue Jan 2 21:35:46 2018 +0530

Created the file

git log command is used to show the revision history of the repository. If any time your control stuck up in using log command use :q. It shows in the last commit first format.

To get details about the last commit, we use git show command:

~/Public/GitLearning$ git show
commit 77f6160313e4c72b95d6a3291eefda1be12f915e
Author: gutsytechster <gutsytechster@example.com>
Date: Tue Jan 2 22:59:46 2018 +0530

Added a line

diff --git a/HelloWorld b/HelloWorld
index 5861bb1..2b9c262 100644
--- a/HelloWorld
+++ b/HelloWorld
@@ -1,2 +1,3 @@
 This is the first file in this repo.
 We are learning the Git.
+It is a Version Control System

As we can see, that details include the commit’s hash value, the author, the date, the time etc. The line which has “+” before it(last line), shows that the line has been added, similarly if the line has “-” before it, implies that either it has been modified or deleted. To show the details of last n commits use git show -n, n can be any number. e.g “git show -2“.

Now, let’s try branching. We use branch when we are working on a new feature and we are not sure whether it should be applied to the main project. So, we make a branch and review the feature, if the feature seems to be appropriate, then we merge it otherwise we leave it and delete the branch. So to generate a branch, we use:

~/Public/GitLearning$ git branch NewHelloWorld

git branch <branch_name>, is used to create a branch.We made a new branch as NewHelloWorld. Now to see the number of branch we use git branch command

~/Public/GitLearning$ git branch

As we see, there are two branches NewHelloWorld and master(default branch). The *(aestrik) symbol is placed before the master branch, means that we are in master branch. To switch between branches we use git checkout <branch_name>:

~/Public/GitLearning$ git checkout NewHelloWorld 
M HelloWorld
Switched to branch 'NewHelloWorld'

Now, you can check using the former command if you have switched the branches.

Let’s do a commit here, so first open the file as nano HelloWorld, then do some changes:

This is the first file in this repo.
We all are learning the Git.
It is a Version Control System.

Can you see, what I changed, Yeah you are right, I put “all” after “we”. Now see for git status, then do git add and finally git commit:

~/Public/GitLearning$ git add HelloWorld
~/Public/GitLearning$ git commit -m "Added all after we"
[NewHelloWorld c3246e2] Added all after we
 1 file changed, 1 insertion(+), 1 deletion(-)

Now, see if these changes reflected in the other branch i.e. master. So first use git checkout master to switch back to master and then do git log and you will see that they are not reflected into the master branch.

Now suppose, you liked the changes very much and now you want to apply them into the master, for that we have to use git merge <branch_name>. First you have to switch into the branch in which you want to merge and then apply merge operation.

~/Public/GitLearning$ git checkout master
Switched to branch 'master'
~/Public/GitLearning$ git merge NewHelloWorld 
Updating 77f6160..c3246e2
 HelloWorld | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Now you can see the git log to see the commits which have been done, and you will find that the commit which you did in the NewHelloWorld branch. Since, the branch we have merged is of no use now, so it can be deleted. To delete a branch we use git branch -d <branch_name>:

~/Public/GitLearning$ git branch -d NewHelloWorld 
Deleted branch NewHelloWorld (was c3246e2).

Like this, there are so many other operations also. This was just a small gist of what git is. We’ll see what it can do in upcoming blogs.

Upto then, be curious!


2 thoughts on “What is Git?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s