Signup/Sign In

Getting Started With GIT

Git is a version control system that helps us to better manage our projects. A version control system is a software that tracks changes made to a project and stores all the modifications made to a project by storing different versions of it. Additional information like who made the changes and at what time are also tracked. Git is not the only version control system out there but it is by far the most popular and most widely used one. Any project managed using Git is called a Git Repository.

What is Git used for?

  • A major use case of Git or any version control system is to compare the current version of a project to previous versions and eliminate complicated errors.
  • Another reason why Git is widely used is for collaborating on the same project. This is done with the help of hosting services for Git Repositories like GitHub or BitBucket. We can create a remote repository on sites like GitHub and then push or upload our local Git repository on these sites and allow others to pull or download from this remote repository.
  • Git makes it very easy to roll back changes in our projects by simply reverting to previous versions.

How does Git work?

  • Git manages the different versions by maintaining a series of snapshots of the project. Whenever we want to commit or store a change permanently a new snapshot of our current version is taken and stored by Git.
  • Some version control systems store the differences in the versions instead of storing the actual version but this proves to be inefficient as every time we revert to a version we will need to reconstruct that version of the project using all the previous differences.
  • Each version of the project that is committed points to the version that was committed before it and refers to it as its parent. This makes reverting to previous versions easier.

Structure of Commits and snapshots

As shown in the illustration above, the first snapshot(Snapshot A) is the very first commit that has been made and so it has no parent. When another commit is made(Snapshot B), it points to the initial commit and refers to it as its parent. Similarly, the commit captured by Snapshot C points to Snapshot B.

Branching in Git

Another important feature of Git is its Branching support. A branch is just a pointer to one of the commits or snapshots of commits that we discussed above. Branches exist so that individual contributors can start from any one of the committed versions and build something on top of it without affecting the original series of snapshots. These changes can then be analyzed and merged into the original series of snapshots. A default master branch exists that always points to the most recent snapshot.

Series of snapshots with the master branch pointer at the latest commit.

Let's consider the following scenario to better understand branching:

  1. There exists an original series of snapshots(blue).
  2. Now, a developer decided to add a new feature so he added a new branch that starts from the initial commit that was first made(purple) and added a new snapshot or a commit.
  3. Another developer decided to build something on top of the second most recent commit and so started a new branch from there(green) and added three new commits.
  4. These new snapshots that were created by the developers do not affect the master branch and give them their own independent space to work the features. Later they may merge these branches into the original master branch.

Branching illustration

Git Workflow

Working with Git consists of moving between three stages. Let's take a look at these three stages.

The Working Directory

The place where we can make changes to our project like adding new files and deleting or modifying the existing files. Whenever we make any change to our files we move back to this stage.

The Staging Directory

The place where we can list the changes that we made to our files. The main aim of this stage is to segregate and organize the files that we have to commit. Changes can be made to multiple files in the working directory but we can add only those files in the staging area that we have to commit. This helps in keeping the project versions simple and easy to understand.

The Repository

The place where Git permanently stores or "commits" these changes as different versions of our project. All the snapshots of the project versions are stored here along with some additional information.

Three stages of the git workflow

Pros and Cons

Even though Git seems like a perfect solution for many problems but it comes with its own advantages and disadvantages. Let's take a look at some of them:

Advantages

  • Promotes Open Source Softwares.
  • Git works on a Distributed Model which means that all changes and updates to the repository will be visible to anyone who clones it. However, the owner has the control to make the repository public or private.
  • Git works seamlessly on almost all the major platforms and is faster than other version control systems.
  • Git provides excellent branching and merging features.

Disadvantages

  • Git provides mediocre support for binary files like images and multiple changes to such files make Git a bit cumbersome to use.
  • Git has a steep learning curve and one needs to put in the time and effort to master it.
  • Git is somewhat slower on the Windows platform as compared to other platforms.

Summary

Git is a powerful Version Control System used to manage projects. Git uses the concept of taking snapshots of our projects and storing them as commits. Git also provides excellent branching support which gives developers the freedom to work on a feature in an independent environment. We learned about the three stages of Git Workflow and what these stages are used for. We also discussed some important pros and cons of using Git for version control. I hope this article was helpful and you learned something new. In the next tutorial, we will learn how to install Git on your PC.



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight