What is Git?

Learn via video courses
Topics Covered

Overview

Git is a version control system that tracks the changes in the code, documents, and other important information regarding a certain code base (or project), etc. Git is free and one of the most widely used version control systems. We can use Git through the command line and its graphical user interface (GUI). The command line or terminal version of Git is known as Git Bash; the GUI version of Git is known as Git GUI. Git tracks the changes in a project and saves a certain state known as commit. A commit is a snapshot of the file's current version (s). So, we track these commits and can revert to a certain commit if we want.

Pre-requisite

To be honest, there are no prerequisites to learning git commands. The basics of git and basic git commands are fairly easy to learn. Some git concepts and commands are complex, but more practice can make things easier. Now, Git is a version control system, so we should have a basic understanding of the version control systems.

Version Control System

A version control system is a tool in software development that tracks the changes in the code, documents, and other important information regarding a certain code base (or project), etc. There are two types of Version Control systems, namely - Centralized Version Control Systems (CVCS) and Distributed Version Control Systems (DVCS). Git is a distributed Distributed Version Control System.

As the name suggests, the Centralized Version Control System used a centralized server to store all the code base and other necessary files. The central server acts as a single directory (or repository) that the user or his/her other team member(s) can directly access. On the other hand, in the Distributed Version Control System, every user has a clone (local copy) of the main central repository.

Introduction to Git

Git is free and one of the most widely used version control systems that Linus Torvalds developed in 2005. Git can extensively handle changes in small to very large projects. Git smartly monitors the changes in our project (whether small or large) and helps us keep track of these changes made in the source code of our project. Git saves the various versions of our project into a special folder named Git repository. The Git repository consists of files having different versions of the project. To make Git track a certain project, we must first initialize the project folder. After the initialization, Git starts tracking the project folder.

To understand the need for Git and any other version control system, we should first visualize the scenario without Git.

Let us suppose that a team is working on a project (for example, a game development). Since a team is working, everyone should have an exact copy of the source code. Now, whenever a team member makes any kind of change in the project, he/she must update the source code for every other team individual. So, this is a hectic task to be carried out each time.

Let us take another difficult scenario of the same project. As the team was working very hard on the project, they made a good game and published (deployed) the game. After some time, a team of small developers wanted to add certain functionality, so they updated the source code, but the overall game got hampered due to some mistake or bug. So, how will they rectify the problem in the massive source code, and for the time being, how can they roll back to the previous stable version?

Now all these and many other problems in project development have one solution, i.e. version control system, particularly Git.

Features of Git

Let us look at some of the features of Git which make it one of the most widely used version control tools:

  • Git is free software.
  • Git is used to track the changes made in the source code.
  • Multiple users can work together using Git.
  • We can create multiple branches in Git, which makes development easier.
  • Git creates a backup of our project so that we can get the source code in case of data loss.

Ways to Use Git

We can use Git through the command line and its graphical user interface (GUI). The command line or terminal version of Git is known as Git Bash; the GUI version of Git is known as Git GUI. Refer to the images below to have a good understanding.

ways to use git example

ways to use example output

How to Get Started with Git

Let us now learn how Git is used right from the installation.

Git installation

The installation of Git is pretty simple.

  1. You need to visit the Git download page here.
  2. Click on the Git installer for your operating system, and then the download will be started.
  3. Open the Downloads folder, click on the git package, and install it like any other application.

After the application, we first need to configure our username and email addresses. So for configuration, we can type the following commands:

Configuring is one of the most basic and primary steps of using any application. So, to configure Git, we use the git config command. We need to specify our user name and email address along with the git config command so that both can be used in our future **git commits **(a commit is a snapshot of the current file version).

Command:

Note: The above command, --global, is a flag.

Terminal vs GUI

We can use either the terminal of Git (i.e. Git Bash) or Git GUI as both are the same. Now, in the terminal version of Git, we need to learn the commands. On the other hand, if we use the GUI Git, we do not need to learn the command, and we can simply click on buttons to perform the desired action(s).

How to Prepare your Project Folder in Git?

Now if we want to track a certain project by Git, we need to open the project folder using git (open the project folder, then right-click and select the option Git Bash Here or Git GUI Here). Alternatively, we can navigate to the folder using Git bash as well.

Now, we need to initialize the Git Repository so that Git can track the changes; let us learn briefly about the Git repository and, later, how git tracks the changes.

What is a Git Repository?

A git repository is a directory named .git in our project folder. The repository keeps track of the changes in our file and the project. It stores the change history so that we can revert to certain changes.

Now, to initialize a git repository, we need open Git Bash in our project repository and type the command:

git repository

When we run the above command, all the files and directories needed for Git to keep track of everything are created. These necessary files are kept inside a hidden directory, namely .git.

Note: The init command is an abbreviated form of initialization.

By the term initialization, we mean that the git repository must be created as this repository will contain the files that will ultimately have the record of versions of our project.

How to Collaborate with Other Developers Using Git

If we are collaborating with another developer (s) on a project, then Git can be used to do so. Other fellow developers can copy our project from the remote repository (generally a central repository like GitHub).

By cloning the project from the remote repository, fellow developers will have a copy of the exact project on their local machines. Other developers can make the changes and then push (or, in simple terms, upload) the changes to the central repository. In this way, all the team members can collaborate.

Git uses the SHA-1 algorithm for internal checking and to name objects. The name of an object is the concatenation of its type, length, null byte, and the object's SHA-1 content. So, Git relies on the SHA-1 hash algorithm for checking all the file objects and the commits.

How Does Git Track Change?

Git tracks the changes in a project and saves a certain state known as commit. A commit is a snapshot of the file's current version (s). It saves a log message and a unique commit id of the current modifications made to the git repository. When we use the git commit command, the changes are saved in our local system, along with a short message related to the commit is also stored, which is provided by the user along with the command.

So, we track these commits and can revert to a certain commit if we want.

Branches and Commands in Git

A branch is an independent line of development used to add certain features and fix bugs without hampering the main project. So, we can develop new features in parallel, and when the development is completed, we can add them back to the main project.

The git branch command enables us to perform parallel development. The command can create, rename, list, and delete branches.

Command for creating a new branch

Command for listing all the branches

or

Other important git commands are:

  • git checkout
  • git push
  • git pull
  • git log
  • git fetch
  • git merge, etc.

What are the Benefits of Git?

The benefits or advantages of Git are:

  • Git is free and open source, so anyone can use it for the development process.
  • Git allows multiple collaborators to work on a project.
  • Git performs well when it comes to version control systems. It is comparatively faster than other version control systems.
  • Git manages the versions of the directory and handles the security using the cryptographic method SHA-1.
  • Git allows us to push the work to the central repository like GitHub.
  • Git provides us with the facility of branching so that we can parallelly develop some parts of the project without hampering the original code base.

Conclusion

  • What is Git? Git is a version control system that tracks changes in the code, documents, and other important information regarding a certain code base (or project), etc.

  • Git is free and one of the most widely used version control systems. We can use Git through the command line and its graphical user interface (GUI).

  • Git tracks the changes in a project and saves a certain state known as commit. A commit is a snapshot of the file's current version (s).

  • We can create multiple branches in Git, which makes development easier.

  • Git creates a backup of our project so that we can get the source code in case of data loss.

  • A git repository is a directory named .git in our project folder. The repository keeps track of the changes in our file and the project.

  • Git performs very well when it comes to version control systems. It is comparatively faster than other version control systems.

  • Git manages the versions of the directory and handles the security using the cryptographic method SHA-1.

  • Git allows us to push the work to the central repository like GitHub.

  • Git provides us with the facility of branching so that we can parallelly develop some parts of the project without hampering the original code base.