Git Tutorial For Beginners: Learn Git from Scratch

Developers use a version control system to ensure that the source code remains intact.

·

7 min read

Protecting the source code is a crucial part of the software development process, especially when complex code is involved since developers must understand the issues or bugs that have been fixed. Therefore, many software developers use a version control system to ensure that the source code remains intact. It is nothing but a set of software tools that allow developers to track and review the changes made in the software codes. One such version control tool is the GIT.

What is GIT?

Before you understand what GIT is, it is essential to know the different types of version control systems. It is mainly of three kinds – local, centralized, and distributed. Developers commonly use the distributed version control system for projects that require large bandwidth. It facilitates tracking the changes made to the code when multiple developers work on the same code.

GIT is a distributed version control system that efficiently tracks and stores changes in large-scale projects that involve several versions of a single code. One of the significant advantages of using GIT over other distributed version control systems is its speed. Developers can immediately retrieve a modified version of the code because of the parallel branching mechanism in GIT.

Basic Functioning of GIT

The storing procedure in GIT consists of three steps, namely, committing, modification, and staging. However, before you understand the different states of GIT, you should be familiar with the concept of repositories. They serve as the core element of version control systems. It is similar to a data folder or data structure that stores various modified files. Repositories are available both on a local network through the Internet and on a remote server.

First, you need to add files to the repository for storage on the local database. This is known as committing the files on GIT. The next state of modification is when changes are made to a file but not stored on a repository. It is called staffing which involves marking the modified file to move it to the local repository. Finally, when the files are committed in the local repository, they move to the remote repository.

To record the changes in the remote repository, you need to take the following steps:

To begin with, you need to add the modified files to the local index or database. Then commit the files to the local repository. Then, you need to use a different viewer tool to track the changes made in the file stored in the repository and new changes. The entire process is carried out through the different sections in GIT – the Git directory, the working directory, and the staging area. One of the most important sections is the GIT directory, where a file’s metadata is saved. When you copy a file from a remote repository, the compressed metadata helps identify the file and understand the changes.

The working directory acts as a checkout step from the GIT directory. It decompresses the files stored in the initial database and transfers them to a disk to allow programmers to use or modify the file.

Last is the staging area that works as an index. It decides which file shall go next for commit. Therefore, the GIT process involves modifying files, staging them, and committing them to the repository.

GIT Terminologies

Blobs:- In GIT, the binary large objects are caller blobs. They represent the file version. Even though blobs comprise file data, they do not have the metadata.

Trees:- In GIT, trees can be understood as objects representing a directory. They hold various file versions or blobs.

Commits:- It represents the current state of a repository. Commit objects include a pointer that allows the developer to go to the parent commit and track history.

Branches:- GIT has a master branch by default. However, you can also create sub-branches to work on a different feature. Once you are done working on the feature, you can merge it with the main branch.

Tags:- Developers use tags to give a specific name to a particular version of repositories. They are similar to the branches. However, you do not have to modify a tag even while creating a different commit.

Clone:- It mirrors the complete repository and checks out the working copy.

Pull:- This operation is used to synchronise changes between the local and the remote repositories. It copies the changes made in the remote repository to the local one.

Push:- Contrary to the pull operation, the push command copies changes made in the local repository to the remote one.

HEAD:- You can understand HEAD as a pointer in GIT that points towards the latest commit. Whenever a new commit is made, the HEAD gets updated automatically.

Revision:- These are known as the source codes or the previous version of the commit.

URL:- It is a link that represents the location of the GIT repository.

GIT Basic Commands

Here are a few GIT commands that you must remember to perform the core functions in GIT:

GIT help command:- This command aims to find out information about other commands. In simple words, if you do not know what a particular command does or how it works, you can use the GIT help command to get information.

GIT Clone:- You can use the clone command to create a snapshot of the remote repository on the local database.

GIT Fetch:- This command shows the latest changes in the remote repository.

GIT Commit:- It creates a snapshot of the files in the local index that need to move to the remote repository. Click to learn more about Git Commands.

Step by Step Guide to Learn GIT

How to download and install GIT on Windows?

There are various ways to install GIT on Windows. Since GIT is an open-source platform, you can download the latest version GIT directly from the official website for free. Another option is to install GitHub on Windows, which already has a command-line version. The third option is to install GIT from the source directly. However, you will also require GIT libraries like expat, Zlib, and curl for this purpose.

Setting up

After downloading or installing the GIT software on your desktop, you have to customize the GIT environment by setting the GIT configuration tools like setting identity and editor, checking the personal settings, and getting help.

To set the identity, you need to enter your name and email address that the GIT commits will use. The next step is to change the GIT editor. If you wish to use the default editor in your text, you do not have to configure the GIT editor.

You can check all your settings on GIT in one go by using the config list demand. It will show you all the settings that you have made on GIT.

Creating or initialising repository in GIT

You can start using GIT after you create or initialise a repository. First, you need to go to the directory in which you want to create a repository and open up the terminal (Git CMD). Now, you have to execute the command ‘git init’. The command creates a folder called ‘.git’, which serves as the repository’s metadata.

Cloning a repository

You can access a remote repository on the local system by cloning it using the command .git clone . You can work on the local file, make changes and then copy the changes to the remote repository using the push command.

Creating a commit

Commit is a snapshot of the history of all the changes made in the repository. You have to choose specific files that need to be committed. The committing process involved adding references or messages for the changes you have made to understand the modifications later. To commit a file, you have to open the repository and execute the git commit command. A text editor appears on the screen. Next, you need to type a commit message. It is best to follow the 50/72 rule while writing the message. It means writing a 50 characters summary in the first line and using 72 characters for the explanation.

Viewing history

You have to use the ‘git log command’ to view the history of a repository. It will show you all the changes made to the repository in reverse chronological order, the author’s name and email, the commit message, and the exact time of creation.

Creating and merging branches

GIT allows working on different sub-branches for performing various functions. You can create a new branch by executing the command git branch . To merge the sub-branches with the master branch, you need to place the head in the master branch. It indicates that the sub-branch will move there. Then, you need to execute the merge command git merge .