Introduction to Git and GitHub

Git is a version control system. Version control system keeps track of changes to a file or files over time. This feature helps us to go back to a specific version later.

Why use Git?

During the process of software development, the files pertaining to the project undergo changes from time to time. This results in different versions of files being created. If we try to manage the project manually we face many problems.

The problems that we encounter here are as follows

  • Comparing changes across versions is hard.
  • Making multiple copies makes it prone to naming and other errors.
  • Hard to share these copies with other people and work on them.

Git provides us a way to deal with these problems. By knowing a set of commands we can easily manage our project.

Git makes it easy to keep track of changes to files and folders, collaborating with other people, reverting back to a previous version, and also trying things out without affecting the original copy of files.

Let us understand a very important term related to Git which helps in creating different versions of a software project i.e. Commit.

Commit

In git, a new version of the project is created by taking a snapshot of the entire project directory. This snapshot is known as a commit. A commit saves the changes of a file at a particular point in time.

When to make a commit?

A commit is made when a particular task relating to the project is completed. This can be completing a feature, fixing a bug, or some logical unit of work depending on the project.

Stages in Git

Now that we have understood the need for Git, we will try to understand the different stages that a file goes through in this system. Files that are tracked by the Git system goes through the following three stages :

  1. Modified
  2. Staged
  3. Committed

git_stages.png

  • Whenever we make some changes in a file it is modified and remains in the untracked area which is also known as the working tree. This is the place where we create, update or delete files i.e all the modifications happen in this area. Tracking of files doesn't happen in this area.

  • To track the changes we have to move the files to the staging area. This is the preparation area to add changes to our commit. Git gives us control over which files from the working tree is to be brought to the staging area so that it can be put into our next commit.

  • The committed stage keeps the commit-graph. At this stage, the acceptable changes are finally committed. In this stage, git records a snapshot by creating a commit. Here the changes are saved to the repository.

For every tracked file, git records information such as its size, creation time, and last modification time in a file known as index. To determine whether a file has changed git compares current stats with those cached in the index. If they match, then git can skip reading the file again.

Some useful commands in Git

So far we have understood the need for git and the working of git. Now is the time to understand various commands that are useful for working with git. After installing git, the first step is to configure git, which can be done using the following command

  • git config --global user.name "<user-name>"

    git config --global user.email "<user-email>"

--global helps in creating a global setting i.e. setting applied to all git-enabled directories in the machine being used.

  • git init

This command initializes git in the folder where the command is run so as to track changes in the folder.

  • git status

Gives us information about which changes are in which state(untracked, tracked, or committed).

  • git log

Used to view the commit history of a repository.

  • git add <file-name>

Moves the file from the working tree to the staging area.

  • git commit -m <message>

Creates a commit. message can be any relevant message for the commit.

  • git diff <file-name>

To find the difference between the staging area of a file and the working tree.

  • git restore --staged <file-name>

This moves the file from the staged area to the untracked region. Useful when we do not want to commit the changes in the next commit.

Branches in git

While developing software there are a number of features to be added and some issues to be resolved. All these changes cannot be done on the main copy of the project directly as the changes can result in erratic behavior of the software if it is not tested properly before incorporating with the original project. To deal with this situation branches are created. A branch helps in creating a copy of the original project. We can do any changes in this branch without affecting the original copy. Once we are satisfied with the changes and verified them, we can incorporate the changes into the project.

Some useful commands that will help us to work on branches are as follows.

  • git branch <branch-name>

Creates a new branch.

  • git checkout <branch-name>

Switches to the mentioned branch.

Merging of branches There are two ways of merging a branch

  1. Fast forward merge
  2. 3-way merge
    • Fast forward merge happens when there is a direct path from the current branch to the branch that we want to merge it into.
    • 3-way merge happens when there is no direct path between the two branches.

In both cases the command used is

  • git merge <branch_name>

This merges the given branch to the current branch.

After merging a particular branch we do not need that branch anymore as the work of that branch is completed. So, we can delete the branch. This can be done by the following command.

  • git branch -d <branch-name>

In the case of Fast forward merge, no conflict is encountered. But in the case of a 3-way merge, there is a chance of conflict arising if we try to merge branches that have changed the same lines in the same file. Here we have to resolve the conflict manually. Git helps us in resolving the conflict by providing visual markers in the conflicting file.

  • git branch

The above command shows all the branches.

Let me conclude the blog with a brief note on GitHub.

GitHub

Github is a platform where we can store and work on our software projects in a team.

There are various important features of GitHub that help us to work on the project. Github uses git as the version control system. We create a repository for our project on GitHub which can either be public or private. A public repository can be accessed by anyone whereas a private repository can be accessed only by the owner and collaborators in the project.

Git helps in managing the project. We can go to an earlier snapshot of the project. Also, branches can be created to work on new features or issues without affecting the main branch(generally master branch). Github facilitates discussion among collaborators, which is very crucial in the development of a project as a team.

Markdown, a lightweight language for text formatting helps in communicating information effectively, where one can format the text as bold or italics, can use headings, images can be uploaded, links can be created, etc. Markdown is an important feature of GitHub.

We can upload our project to GitHub from our local machine or from other sites irrespective of whether it supports version control or not. But it is easier if the project is on the local machine. In software development, GitHub or some other platform that supports version control is absolutely essential.

So we can say that git is a tool whereas GitHub is a platform that provides service to host git repositories in order to collaborate with other people.

This blog will help you get started with git and GitHub.