Learn Git From Scratch On Linux - The Version Control System

learn git on linux operating system-creationcodes workout examples

Version Control System

When I was new to programming, I remember, while solving a competitive programming question, I use to create files like final.py, this_is_final.py, finish.py.

Silly enough, I assume many of you have done this at some point of time, right? There are many ways to solve a coding question, though they say that if a program works, don't touch it, A coder likes to tweak all the ideas he got while writing a script.

So, In order to get rid of these files, the concept of the Version Control System came into play. Though the use of version control is vast enough, I am explaining the idea as easy and relative I can...

We use the Version Control System to track different coding versions of a file, modify them or allow contribution or collaboration with other programmers on our project.

Git is a version control system originally written by Linus Torvald The great... He is like a godfather in the open-source community.

Imagine Git as  "The Eye" in the movie Lord of the rings. The Eye watches everything...

But, our Lil friend Git watches different versions or changes we made to our code and keeps track of all the changes we are making, giving us the ability to roll back to the original code if things go the wrong way.

Isn't this super helpful, You tweak and tweak and tweak with your program and eventually you forgot what was the original code? But then you do one command and the original code comes back, wooosh.

Installing git in Linux

Generally, all the Linux environments come with git pre-installed. You can check whether it's available to you or not using the command:

git --version

If the git is installed, it should show you an output with the version of git you are using.

git version 2.25.1

If the git is not installed, you can install it using the get-apt or apt command, use your package manager based on your Linux distribution, I use Debian, so the command is

sudo apt install git

Repository In git

The repository is nothing but a directory you want the git to keep observing for any version control or code changes you made. It can be your project directories or any other directory you want to specify.

To let the git know about the directory you want it to observe, or in technical words to create a repository
just type

git init <directory>

For Example, I want to create a repository named "gitDemo" in my desktop, I will write:

git init /home/soul/Desktop/gitDemo

This command then creates a folder named gitDemo.

Let's look into what are the contents of this folder

ls /home/soul/Desktop/gitDemo

When you type this command, you will see that the folder is empty. But wait, there should be git configuration files or something like that must be present in that, This is why I call the git as The EYE.

There is a special file, in that folder named .git/, as this file starts with "." the normal ls command will not show this to us.


ls -a /home/soul/Desktop/gitDemo


.  ..  .git

This is the place, where all the git files were kept.

If you do an ls to that directory,

ls home/soul/Desktop/gitDemo/.git/

you will see folders

    branches  config  description  HEAD  hooks 
    info  objects  refs  
Now, Come back to our discussion of the repository.

Repositories in the git are like a workbench, whatever file you put in that repository, git will keep track of any changes you were making to that file.

So, Let's create a quick python file in that repository.

cd /home/soul/Desktop/gitDemo

nano demo_script.py

#!/usr/bin/env python3

print("Hello, this is my first program to understand how git works")

Create a file demo_script.py using the above code.

Now, some really important topics are coming up, From here we will be going to understand how git manages to keep track of the changes we make.

Staging Area In git

So far we have created a file named demo_script.py, Now what? How do we know that the git is tracking those files? And what does it means to track a file?

For a better understanding, type this command:

git status

This command is used to check the status of what is our Lil friend git is up to?


On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

nothing added to commit but untracked files present (use "git add" to track)
Don't panic, I know we have not discussed the terms branch master, no commits yet, just ignore those lines and pay attention to the line:

Untracked files: 
     (use "git add <file>..." to include in what will be committed) 
 nothing added to commit but untracked files present (use "git add" to track)

The above statement is telling us that there are some untracked files that exist, and the name of the file is demo_script.

It further tells us that to help git track your files, type the command git add <the file name>.
So, just type the command

git add demo_script.py

Now, what? The git will capture the state of this file, It's like taking a screenshot, If you do any change in your working tree or working bench or working directory, The git matches the file state and tells you that some changes have been done.

This is what we call the Staging area.

When you type the command git add <file name>, that file goes to the staging area. The staging area is where the git keeps track of our files.

Now, to see the above-discussed terms in action, Let's make some changes to our script.


nano demo_script.py

and add this line to your file:

print("I am going to modify this file")

Now, to see that the git notices this change or not, type the git status command.

git status


  On branch master
  No commits yet
  Changes to be committed:
    (use "git rm --cached <file>..." to unstage)
    new file:   demo_script.py
  Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working
    modified:   demo_script.py

Observe, the line:

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
modified:   demo_script.py

This line is saying that we have not committed our changes, and moreover, the file demo_script.py is modified.

Now, to know what is modified in your script, you can see it with a command git diff <file name>

git diff demo_script.py


  diff --git a/demo_script.py b/demo_script.py
  index 0177262..cdd5291 100644
  --- a/demo_script.py
  +++ b/demo_script.py
  @@ -1,2 +1,2 @@
   print("hello, this is my first demo to how to work in git")
  +print("I am making a change in this script")

The + sign command tells us the changes we have made.

So, Up till now, I think you have understood the concept of the staging area in git and how to check the status of git.

If you want git to track your files, you should put that file in the staging area, using the command git add.

Now, what ahead? When we are satisfied with our work, we need to commit the changes we made.

Committing changes In git

To commit the changes we make, use the command

git commit

This will open a text editor that tells you to write something about this commit, So whenever you want to rollback or see the history of commits, you will get an idea about how and why we committed these changes.

Now again, when you check the status using the git status command,

git status

you will get this result:

  On branch master
  nothing to commit, working tree clean

Branches In git

Up till now, you have seen many times the word branch master has come up. What does this mean?

Let me put the concept like this:

Ever heard of parallel space? Parallel space is always an amazing section of science fiction movies, where there is a whole new universe like us exists having our doppelgangers doing some amazing and interesting shits compared to us...

Branches in git are like that parallel space. When you create a branch, you are actually creating a parallel space for your work.

Why we do that? The main brach is known as "Master branch". We don't want to mess with that master branch right?

When you work with so many people and collaborators, you don't wanna mess with the main branch codes. Instead, you will create your own branch and do your shits and experiments on that branch.

And If things work out the right way, you will request a merge to your work with the master branch.

To create a branch, the command is  git branch <name of branch>

git branch newBranch

This will create a new branch with the name "newBranch". Now do whatever you want in this branch. This branch is like you parallel space. when you are done with your work, just request a merge to the master branch.

To switch between branches, The command is git switch <branch name>.

git switch newBranch

Let's give you a quick demo on how the branch concept works. Switch to the new branch we have just created.

Edit the file demo_script.py by writing an another line of code, and commit the file to make changes.

These changes are saved in the branch "new branch", And the master branch do not know about this change.

To merge the work of both branches, we have to switch to the master branch. Type the following commands:

git switch master
git merge newBranch


  Updating 2e9b6ff..080577e
   demo_script.py | 1 +
   1 file changed, 1 insertion(+)

Working with logs in Git

This is the final topic we are going to discuss in this post.
While working with so many collaborators, There is a chance that we can commit a buggy file.

Whenever we commit any changes to the git, It records every commit in a log file. Each entry in the log file contains the name, date, commit number and email ID of the developer.

To watch the log file of git use the below command

git log


  Author: srujan <srujan@gmail>
  Date:   Mon Aug 17 17:56:36 2020 +0530
  commit 2e9b6ff9bbb6b3337716c59fd1f1150e0c86386a
  Author: Srujank0903 <creationcodes@gmail.com>
  Date:   Mon Aug 17 17:21:44 2020 +0530
  commit 3c84d1b43c1684ba4cda212ae7142dfa8d137792
  Author: Srujank0903 <creationcodes.wow.com>
  Date:   Mon Aug 17 17:02:47 2020 +0530

This log file contains all the details including the commit id. we can use the commit ids to roll back to that particular point of time.

To roll back to a particular commit type the command:

git revert <commit ID>

The above discussion is all about using git locally on your computer or laptop. The above discussion is the basic guide to getting your hands on git and understand the concept as easily as it could be.

The main concept of git is just this, It tracks all the different versions of your code, Helps to keep the changes organized while working with huge organizations and people.

I hope you find this post interesting and helpful.

For now,
Peace out,
Do subscribe and follow the blog if you like.

Post a Comment