Understanding Git and Version Control Systems

Nisal Pubudu
May 14 · 7 min read
Image: Version Control (https://blog.cpanel.com/)

Git is a free, open source distributed version control system tool designed to handle everything from small to very large projects with speed and efficiency. You might be a web developer or app developer; git is a useful tool for anyone who writes code or track changes to files. Git has the functionality, performance, security, and flexibility that most teams and individual developers need.

It also serves as an important distributed version-control DevOps tool. So, in this article I will explain about git, it’s features and functionalities. But before moving into git, we should have basic understanding about Version Control Systems (VCS).

Version Control Systems

Version control systems are a type of software tools that helps in recording changes made to files by keeping a track of modifications done to the code. As software engineers, when we develop a software product, most of the time we collaborate as a group. So, the thing is these group of software developers might be located at different locations and each one of them contributes in some specific kind of functionality or features. So, in order to contribute to the product, they made modifications in the source code. So, this version control system helps developers to efficiently communicate and manage(track) all the changes that have been made to the source code along with the information like who made and what change has been made.

Version Control Systems can be divided in 2 categories,

  • Centralized Version Control System (CVCS)
  • Distributed Version Control System (DVCS)

Centralized version control systems contain just one repository and each user gets their own working copy. You need to commit to reflecting your changes in the repository. It is possible for others to see your changes by updating. In CVCS, two things are required to make your changes visible to others which are, commit and update.

Even though it seems pretty good to maintain a single repository, it has some major drawbacks like,

  • It is not locally available: So, you always need to be connected to a network to perform any action.
  • High probability of loosing data: Since everything is centralized, in any case of the central server getting crashed, it may result in losing the entire data of the project.

Distributed version control systems contain multiple repositories. Each developer has their own repository and working copy. So, that means even you do some changes by committing, other developers have no access to your changes. This is because commit will reflect those changes in your local repository, and you need to push them in order to make them visible on the central repository. Similarly, when you update, you do not get other’s changes unless you have first pulled those changes into your repository. In DVCS, there 4 main operations named as, commit, push, pull, and update.

If you use DVCS you can have following advantages over CVCS.

  • All operations (except push & pull) are very fast because it only needs to access the hard drive, not a remote server. Hence, you do not always need an internet connection.
  • Committing new change-sets can be done locally without manipulating the data on the main repository.
  • Since every contributor has a full copy of the project repository, they can share changes with one another if they want in any case.
  • If the central server gets crashed at any point of time, the lost data can be easily recovered from any one of the contributor’s local repositories.

What is Git?

So, as you already know, git is a free, open source distributed version control system tool designed to handle everything from small to very large projects with speed and efficiency. After reading about Version Control Systems you might already guessed that Git is a Distributed Version Control System (DVCS). So, git comes with all the Distributed VCS facilities to the user that was mentioned earlier.

Popular Question:
What is the difference between Git vs GitHub?
Git is just a version control system that manages and tracks changes to your source code whereas GitHub is a cloud-based hosting platform that manages all your Git repositories.

Image: Version Control using Git (https://www.simplilearn.com/)

Features of Git

A Git repository contains all of the project files and the entire revision history. You can take an ordinary folder of files and tell Git to make it a repository. This creates a “.git” sub folder, which contains all of the Git metadata for tracking changes.

A commit logs a change or series of changes that you have made to a file in the repository. A commit has a unique SHA1 hash which is used to keep track of files changed in the past.

Before we make a commit, we must tell Git what files we want to commit. This is called staging and uses the add command. Why we have to use add instead of committing the file directly? Let’s say you’re working on two files, but only one of them is ready to commit. So, you do not want to commit both files, just the one that’s ready. That’s where git’s “add” command comes in. We can add files to a staging area, and then we commit the files that have been staged.

Image: Branches in Git (https://www.simplilearn.com/)

A branch is essentially a unique set of code changes with a unique name. Each repository can have one or more branches. The main branch is where all the changes eventually get merged into and it known as the master (main). Lets see how useful git branches with an example.

  • Assume that you need to work on a new feature for an application. To start work, you have to first create a unique branch.
  • So, while working you get a request to make a quick change that needs to go live on the application today. But the thing is you haven’t finished your new feature.
  • So, what you can do is, switch back to the master branch, make the change, and push it live.
  • Then you can switch back to your new feature branch and finish your work. When you’re done, you merge the new feature branch into the master branch and both the new feature and that quick change are kept.

When you merge two branches you can sometimes get a conflict. For example, you and another developer unknowingly both work on the same part of a file. The other developer pushes their changes to the remote repository. When you then pull them to your local repository, you’ll get a merge conflict. But git has a way to handle these type conflicts, so you can see both sets of changes and decide which you want to keep.

Image: How Pull Request work (https://guide.quickscrum.com/git-guide/)

A developer calls a pull request to ask another developer to merge one of their branches into the other’s repository. Besides making it a lot easier for project leaders to monitor and track code changes, “pulling” also facilitates other developers to discuss their work before integrating the code with the codebase. Moreover, if a developer can’t continue his work for some reason, he can initiate a pull request to seek help from the rest of the team.

Common Git Commands

This command sets the author name and email address respectively to be used with your commits.

git config –global user.name <“name”>git config –global user.email <“email address>”

To get started with Git, go to your terminal and run the following command in your project directory. This will initialize a project directory.

git init

This command is used to obtain a repository from an existing URL.

git clone <url>

Run the following command to add files for Git to track. This will add these files to the staging area.

git add <filename>

This command adds one or more to the staging area.

git add *

Run the following command to commit your changes to these files.

git commit -m “<add a commit message here>”

This command commits any files you’ve added with the git add command and also commits any files you’ve changed since then.

git commit -a

We can push our changes through once we’re done.

git push

This command shows the file differences which are not yet staged.

git diff

This command shows the differences between the files in the staging area and the latest version present.

git diff –staged

This command unstages the file, but it preserves the file contents.

git reset <file>

This command lists all the files that have to be committed.

git status

This command is used to list the version history for the current branch.

git log

This command lists all the local branches in the current repository.

git branch

This command creates a new branch.

git branch <branch name>

This command merges the specified branch’s history into the current branch.

git merge <branch name>

So, this is the end of the article and I hope you enjoyed it. Happy Coding👨‍💻.

Ahmed, R., 2020. What Is Git | Explore A Distributed Version Control Tool | Edureka. [online] Edureka. Available at: <https://www.edureka.co/blog/what-is-git/> [Accessed 13 May 2021].

Kappagantula, S., 2020. Top 20 Git Commands With Examples — DZone DevOps. [online] dzone.com. Available at: <https://dzone.com/articles/top-20-git-commands-with-examples> [Accessed 14 May 2021].

Guide.quickscrum.com. n.d. What is Git? What benefits does Git offer?. [online] Available at: <https://guide.quickscrum.com/git-guide/> [Accessed 14 May 2021].

Nerd For Tech

From Confusion to Clarification

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/. Don’t forget to check out Ask-NFT, a mentorship ecosystem we’ve started

Nisal Pubudu

Written by

Associate Software Engineer at Virtusa

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/. Don’t forget to check out Ask-NFT, a mentorship ecosystem we’ve started

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store