DEV Community

Cover image for *Beginner Friendly* Version control using `git`
Ankur Singh
Ankur Singh

Posted on • Updated on

*Beginner Friendly* Version control using `git`

Introduction

Welcome 👋 to this blog. Did you hear about version control and want to learn more or do you think version control is the difficult thing? Then this blog is for you and believe me, at the end of this blog you get the basic idea of version control and you also got some hands-on experience on version control using git.

Let's get started 😇

Let's talk about it in some general way. Suppose we are graduation students and currently pursuing our graduation. One day in our class Professor give us homework and give us a timeline of 1 month. That means we have to submit the homework after 1 month. As all of us have other subjects to study we can complete the homework in pieces of chunks e.g. do a small portion of homework each week.

You start working on the homework and make a file say homework.txt and start working on it and come again in next week, open the file and notice there is some mistake in the homework.txt so decided to make a new file say homework1.txt and continue your work again. And the process going on. After the homework, you may end up with this type of file structure.

file structure

You will feel how hard it is to keep an eye on each file e.g. which files have which type of content. That makes a lot of headaches for us. If there is some version control technology that helps us to keep track of the file changes then it will be very easy for us to manage our homework file. Then git will come into the picture, it allows us to keep track of our files.

git

Git is an extensively utilized version control system that developers employ to track and manage files within large projects, particularly when multiple developers are collaborating.

To install git into your computer go to here.

  • To confirm the installation of git, run the following command
$ git --version
Enter fullscreen mode Exit fullscreen mode

This command will return the version of the git installed on your computer.

Initialize a git in the folder

Create a new folder and name it say folder and open this folder in your favourite code editor. Open the integrated terminal of your code editor.

Note: Please confirm your path, the path may be something like that /Desktop/folder based on your folder name.

Then create a new file say homework.txt (Our ultimate goal is to complete the homework 🤣). Then it is time to initialize the git. Go to integrated terminal and run these commands

$ git init
Enter fullscreen mode Exit fullscreen mode

Output is like: Initialized empty Git repository in /home/ankur/Desktop/folder/.git/
This means you have successfully initialized git in your folder 🙌
This command creates a .git folder on the same hierarchical level where you run your command. Run this command to verify that the .git folder is present.

$ ls -a
Enter fullscreen mode Exit fullscreen mode

Note: git status is a command which helps us to tell the status of our folder or directory or repository e.g. In which files our changes(addition or deletion) has happened.

git status output

Adding a file to a staging area

The next step is to add a file to the staging area. By staging area I mean you can suppose this as a place where you can keep track of all kinds of changes in the file. To add a file to the staging area run the command

$ git add <FileName>
Enter fullscreen mode Exit fullscreen mode

In our case git add homework.txt. After running this command our homework.txt file was added to the staging area. Now any changes can be tracked.

The output of git status is followed after running the command

git status output

Adding commit

Adding commit simply means save the changes with a message. This helps us to maintain a good history of change in our folder or directory or repository.

To add the commit run the command

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

-m - this is a message flag
your message - replace this with any message you want

git commit output

Here I am going to write in the txt file and make some commit. This can be done in the following steps

  1. Make the changes
  2. git add <FileName>
  3. git commit -m "your message"

Here is the pictorial representation what this look like

pictorial representation

If you run the command git log It will basically print out the log of all commit with commit hash you can refer commit hash to unique identity to each commit message.

git log output

**If you are reading till now then I appreciated your dedication 🫡

Revert or switch back to any previous commit

This is the most important part of version control. Suppose you want to go to version v1 version 1 of the commit then you can go there with a simple one-line command for that git checkout <commitHash>

You can find the hash by running the git log command and copying the hash you want to go back to.

git log output

And then run the command. Like this

homework.txt

git checkout output

This is how you can get into the past via commit hash.

You got it 🤩

You have learned about the fundamentals of version control in this blog. Thanks for reading till the end. If you have any feedback, the comment section is yours.

Hire me: ankursingh91002@gmail.com
LinkedIn
Twitter

Top comments (0)