*Beginner Friendly* Version control using `git`
Tags: beginners, tutorial, programming, git
Published: 2023-08-13 17:58:17

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xcpzrvgsbs9uw3t6h28k.png)

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](https://git-scm.com/downloads).
- To confirm the installation of git, run the following command
```bash
$ git --version
```
> 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
```bash
$ git init
```
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.
```bash
$ ls -a
```

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hl0zo68kfdcigyyzwj9p.png)

## 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
```bash
$ git add <FileName>
```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jkg8a7s4ytl5i9ilnme9.png)

## 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
```bash
$ git commit -m "your message"
```
`-m` - this is a message flag
`your message` - replace this with any message you want 

![git commit output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2jr78oelv03ff0ep53ii.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aqewd8kt6g6j1s185puh.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zgkxhntn3h2hyz116f1s.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ud5ixfsxwlulvaer3r6.png)

And then run the command. Like this

![homework.txt](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qk177448652hvxhpsz81.png)


![git checkout output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c3qulffk73bwmfpp6llt.png)

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](https://www.linkedin.com/in/ankur-singh-161458227/)
            [Twitter](https://twitter.com/ankursingh91002)