Understanding Version Control System(VCS) & Git (Local Repo)

Issa Sangare
The Startup
Published in
7 min readJan 3, 2021
Photo Credit: @PERFORCE

What’s Version Control System?

Scott Chacon and Ben Straud is their excellent book “Pro Git, define version control as: a system that records changes to a file or set of files over time so that you can recall specific versions later. It allows you to revert selected files back to a previous state, revert the entire project back to a previous state, compare changes over time, see who last modified something that might be causing a problem, who introduced an issue, and when, and more. Using a VCS also generally means that if you screw things up or lose files, you can easily recover. In addition, you get all this for very little overhead.

There are several VCS tools out there but for this tutorial, we are going to use Git which is one of the most popular VCS tools in use today. Git is a Distributed VCS, a category known as DVCS.

What’s Git?

According to Scott Chacon and Ben Straud: Git thinks of its data more like a series of snapshots of a miniature filesystem. With Git, every time you commit or save the state of your project, Git basically takes a picture of what all your files look like at that moment and stores a reference to that snapshot. To be efficient, if files have not changed, Git doesn’t store the file again. Git thinks about its data more like a stream of snapshots.

Using Git on the Command Line Interface (CLI)

“There are a lot of different ways to use Git, for this lesson we will be using use Git on Command Line Interface. We will expect you to know how to use the terminal, if you don’t know what we are talking about here, I strongly recommend you to my blog post about CLI: How to use the Command Line Interface.

Pre-work

From my home directory, I am going to cd into Desktop directory, create a new directory and then name it my-git-project, after that, I will cd into my-git-project , create a new file and then call it chapter1.txt.

Now, let’s open chapter1.txt and write in it one sentence.

Time to get busy with Git Basics

Repository (repo for short): A directory or folder of files that are tracked by Git.

git init:Initializes a Git Repository

Git operates on a directory level. My current working directory is my-git-project and we want to track our files in. To meet that goal we need to initialize the directory as a Git repository and then Git will pay attention to what goes on in the directory which for this example my-git-project.

Syntax: git init + return/enter

Let’s initialize an empty Git repo inside my-git-project directory .

Since Git is a hidden repo instead of using ls command, this time around we need to use ls -a to list all files and directories even hidden ones inside my-git-project directory.

git status: Checks the status of a repository

Now that we have Git watching my-git-project, let’s use git status command to check the status of our directory. Git showed us, in red, that we have an untracked file presents in our directory, moreover, it even suggested us to use git add in order to start tracking our files. As a reminder, we have already created chapter.txt file in my-git-project directory.

Syntax: git status + return/enter

git add: Keeps track of file changes/ pushes files into a staging area

As you know, the file in my current working directory is not being tracked yet and it’s time to tell Git about all files we want him to keep track of and to do that we are going to use git add command to push chapter1.txt into a staging area.

Syntax: git add <name of the file to be tracked>+ return/enter

We have used git add and git status and it showed us, in green, that we have a new file chapter1.txt in the staging area.

git commit: Creates a commit

At this point, we are going to use git commit to move our file from the staging area into the local repo. As a reminder, our local repo is .git directory that we have already initialized with git init.

Syntax: git commit -m"message" + return/enter. By convention, your message should always be in the present tense. In this example, our message is “initial commit” since it’s our first commit.

Now, if we type git status, we'll see that it is at a "clean state", and there is nothing to commit and no new changes.

git log: The record of what happened in each commit

git add . : pushing several files into the staging area at the same time

In order to use git add . command, we are going to create 2 more files in our working directory and name them chapter2.txt & chapter3.txt.

Now, let’s open up both files and bring some changes in by writing one sentence inside each file.

Now that we have some new files with some contents, let us use git status to check the status. It turned out that, in red, we have two untracked files.

Now, let us use git add . to push those untracked files into the staging area in one go.

Syntax : git add . + return/enter

After using git add . & git status , we could read in the terminal that there are, in green, two files in the staging area.

Now that we have our files in the staging area, let’s push them into our local repo by using git commit command.

Finally, we are going to use git log to see how many commits we have made so far. We can see below our two commits as long as their messages. The second commit message is “complete chapter 2 & 3” and the first commit message is “ initial commit

git diff : Short for “difference,” the “diff” of a file is all the changes that happened in it since the last commit. The “diff” of a repo is all the diffs in all the tracked files in the repo that have been made, but which have not yet been committed.

Before using git diff, let’s bring some changes in our one of our files. In chapter3.txt I am going to manually replace the sentence “Issa is Software Engineer” with “Balla is a freelancer and he does a great job”.

Now that we have changed the content of chapter3.txt let’s use git diff command.

Syntax: git diff <name of the file> + return/enter.

It red, it shows us the sentence which has been deleted and in green the part which has been added.

git checkout: rolling back the file to its previous version

This time, let’s say I want to get back to the previous state of chapter3.txt which was “Issa is Software Engineer”. In order to achieve that goal, we are going to use git checkout command.

Syntax: git checkout <name of the file> + return/enter

After using git checkout , let’s open up chapter3.txt to check out its content, we could see below that we have the original sentence back.

Conclusion: We have just covered some basics commands of Git and I hope you enjoyed reading it. In my next blog post, I will walk you through the relation between Git and GitHub in other words local and remote repo.

Resources :

https://git-scm.com/book/en/v2

https://www.learn.co/tracks/module-1-web-development-immersive-3-0/fswd-prework-2-0/git-getting-and-adding-to-code/git-basics

--

--