DEV Community

Cover image for GIT: Version Control System-Complete Guide
Omkar Sharma
Omkar Sharma

Posted on

GIT: Version Control System-Complete Guide

1. What is a Version Control System (VCS)?

Version control, also known as source control, is a system that helps track and manage changes made to software code over time.

Why is it required?

As a project grows, code is continuously updated—new features are added, bugs are fixed, and existing logic is modified. In such cases, it becomes difficult to answer questions like:

  • Who made a specific change?
  • When was the change made?
  • Why was the change introduced?
  • How can we revert to an older stable version?

Omkar Sharma

A Version Control System solves this by maintaining a complete timeline (history) of code changes, making collaboration, tracking, and rollback easy and reliable.

Famous VCS Tools

  • Git (Most popular)
  • Apache Subversion (SVN)
  • Piper (Used internally by Google)

2. Introduction to Git VCS

Download Git from the official website:

https://git-scm.com/install/

Verify Installation

git
git -v
Enter fullscreen mode Exit fullscreen mode

If Git is installed correctly, it will display the installed version.

Omkar Sharma

What is Git?

Git is a distributed version control system that allows multiple developers to work on the same project simultaneously while keeping track of all changes.

Why Git Configuration is Required?

In an organization, multiple developers work on the same codebase. Git uses global configuration (username and email) to identify who made which changes in the commit history.

Omkar Sharma

Setting Up Git Global Configuration

First let's create a folder where we keep our source code and config the user name and email to it.

Omkar Sharma

git config --global user.name "Your Name"
git config --global user.email "your@email.com"
Enter fullscreen mode Exit fullscreen mode

Omkar Sharma

3. Version Controlling with Git

Initializing a Git Project

By default, a folder is not Git-enabled, and Git does not track it.

Omkar Sharma

Omkar Sharma

To enable Git tracking:

git init
Enter fullscreen mode Exit fullscreen mode

This initializes a Git repository in the folder.

Omkar Sharma

Omkar Sharma

Tracking Files

  • After initialization, files are still untracked
  • You must explicitly add files to Git
git add index.js
git add <File Path Name>
Enter fullscreen mode Exit fullscreen mode

Omkar Sharma

Omkar Sharma

Once added, Git starts tracking changes to the file.

Viewing Changes

git diff
Enter fullscreen mode Exit fullscreen mode

Shows differences between the working directory and the staging area.

Omkar Sharma

Add Multiple Files

git add .
Enter fullscreen mode Exit fullscreen mode

Adds all modified and untracked files at once.

By default, Git does not track any folder or file on your local machine unless you initialize it and add files manually.

Removing Files from VCS

git rm <FILE_PATH>
Enter fullscreen mode Exit fullscreen mode

Introduction to Commits

What is a Commit?

A commit is a snapshot of the staged changes at a specific point in time.

Committing Files

git commit -m "commit message"
Enter fullscreen mode Exit fullscreen mode

Omkar Sharma

Omkar Sharma

Staging Area

The staging area is an intermediate step where files are prepared before committing.

Omkar Sharma

Viewing Commit History

git log
git log --oneline
git diff <to see the changes in code file>
Enter fullscreen mode Exit fullscreen mode

Omkar Sharma

git show <commit id> to see the changes made in particular commit
Enter fullscreen mode Exit fullscreen mode

Omkar Sharma

git blame <File Name> to see the changes made in particular file by whom
Enter fullscreen mode Exit fullscreen mode

Omkar Sharma

Reverting Back

git reset --hard <commit id>

here we are moving our head so we will loose the below commits
Enter fullscreen mode Exit fullscreen mode

whenever we are saying VCS we should have something when we do any thing wrong we can revert back.....so Git allows reverting commits to return to a previous stable state when required.

In this feature we use the concept of Linked List head points to the latest commit.....so we just point our head where we want to revert.

Omkar Sharma

Omkar Sharma

git revert <commit id>
Enter fullscreen mode Exit fullscreen mode

Omkar Sharma

Omkar Sharma


✍️ Author: Omkar Sharma

📬 Feel free to connect on LinkedIn or explore more on GitHub

Top comments (0)