Git Simplified: Fundamental Concepts & Commands
Tags: git, beginners, productivity, programming
Published: 2021-06-27 05:53:42

`Git` is one of the most used tools in the tech industry as it efficiently solves the **version control** issue. Without `git`, even a simple project might end up looking like this:

![Without Git](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jg8o1wn0d6g6jxx60fyv.PNG)

`Git` helps to keep track of different versions of a single code base by tracking all changes. With `git`, even a complex project ([Material UI](https://github.com/mui-org/material-ui)) ends up looking condensed like this:

![With Git](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d5d26yxj89wb7zyox2vy.PNG)

Even though `git` is a really useful tool, everyone (including me) while starting, has a lot of issues wrapping their heads around how it works. This article aims at demystifying `git` for you.

# Installing `Git`

To use `git` you will need to install it. You can download and install `git` from [git](https://git-scm.com/)'s website. Make sure you add `git` to `Path` in case you are using it on **Windows**. In **Linux**, you can directly install it from the terminal using:

```bash
sudo apt-get install git
```

After installation is complete, to check if `git` was installed properly execute the command:

```bash
git --version
```

# How `Git` Works?

`Git` works by storing a snapshot of the entire repository at any given point of time when you **commit** changes (a **commit** can be considered to be an equivalent of a _save_). So whenever you wish, you can go back to any previous commits and make modifications.

# `Git` Basics

### 1. Initializing a Repository

Before you can use any functionalities of `git`, you would require a repository. To initialize a repository use:

```bash
git init
```

### 2. Staging Changes

To **commit** changes, you need to specify the files whose changes you want to **commit** first. This is done by staging the changes. It is NOT required that you stage all files you modified, you can stage only the files whose changes you want to **commit**

To stage changes, use:

```bash
git add <file 01 path> <file 02 path> <...>
```

Or if you are lazy like me, stage all changes using:

```bash
git add .
```

### 3. Committing Changes

Finally, we come to **committing** changes. To save the changes you have staged, use:

```bash
git commit -m "<small description of the change (for ease of understanding)>"
```

You have successfully made your first **commit**!!! 

### 4. Logs

After several **commits** if you require to check out the commits you have made, check out the logs using:

```bash
git log
```

![Logs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yysuc90edj1pcmilw6ok.PNG)

### 5. Resetting & Reverting Changes

But what if I make some mistakes in an old commit 😨???

Fret not my friend, `git` also has two solutions for that:

1.  **Reset**
2.  **Revert**

**Reset**

```
git reset --soft HEAD~1
```

Let's break down the command. We are using `git reset <reset types> HEAD~<number of commits to undo>`

The most commonly used **reset types**s are:

-   **--soft**: uncommit and keep (staged) changes
-   **--hard**: uncommit and delete changes

**Revert**

You might also have noticed that every **commit** is associated with a hash.

![Git Hash](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6gc1xztnj8wdyl3lczhp.png)

You can also use the hash to undo a specific **commit**:

```
git revert 8a11c5095f2dcd70b0bc8c66061a1368558a3abf
```

On breaking down the command, we find `git revert <commit hash>`.

An additional **commit** is added on reverting modifications

![Git Revert Commit](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f4jjg35t7gdl1ar8nyzx.PNG)

Wow!!! We have made it quite far; pat yourself on the back for the progress!

Now let's dive into a few more basic `git` features which are a bit harder to grasp.

### 6. Branches

It is possible that you would be working on multiple different features at once. Scoping all changes in one location is a perfect recipe for disaster for this case, and since you are learning `git`, you definitely would like to keep track of several copies of the same project. 

`Git` has you covered! There is a feature to create branches of the code to scope modifications like _feature additions_, _bug fixes_, etc to only one branch, which may later be merged with other branches. 

The former convention was to call the base branch _master_, but recently the name has been changed to _main_. You can change the name or the base branch as per your requirement though.

![Branches](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xls4372j58kl8kozbvgp.jpg)

To create a new branch use:

```
git checkout -b <new branch name>
```

To switch to an existing branch use:

```
git checkout <branch name>
```

### 7. Merge

After completing work on a branch, you may require updating a branch with the code from another branch. For **merging** changes from another branch, first, move to the branch you want to update and use:

```
git merge <update source branch name>
```

![Git Merge](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c295xb9c1qq9jlk9aqnp.PNG)

### 8. Conflict

Merging branches may result in **conflict** if, in both of the branches, the same part of the same file was updated. In such a case, `git` doesn't know which change to keep and which to discard. So `git` creates a **conflict** message for manual review.

![Git Merge Fail](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eytr3s3bw1d3purmmzx9.PNG)

The conflict message outlines where the **conflict** occurred as well as the current (available in the branch) and incoming changes (merging from another branch).

![Git Conflict](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/egwnkoxah0agycrswz1y.PNG)

After resolving the **conflict**, you need to add a **commit** to save the resolved **merge**.

# Conclusion

In this article, we learned the basic concepts and commands of `git` and how to add it to a project. Now you have this skill in your arsenal and would be able to use it in your projects to maximize your productivity :)

Stay tuned for the article next week, where we will be diving into how to use `git` for remote repositories hosted on sites like **GitHub**.

Finding **personal finance** too intimidating? Checkout my **Instagram** to become a [**Dollar Ninja**](https://www.instagram.com/the.dollar.ninja/)

# Thanks for reading

Want to work together? Contact me on [Upwork](https://www.upwork.com/o/profiles/users/~01c12e516ee1d35044/)

Want to see what I am working on? Check out my [GitHub](https://github.com/ruppysuppy)

I am a freelancer who will start off as a Digital Nomad in mid-2022. Want to catch the journey? Follow me on [Instagram](https://www.instagram.com/tapajyotib/)

Follow my blogs for weekly new tidbits on [Dev](https://dev.to/ruppysuppy)

Connect to me on:

-   [Portfolio](https://tapajyoti-bose.vercel.app)
-   [GitHub](https://github.com/ruppysuppy)
-   [LinkedIn](https://www.linkedin.com/in/tapajyoti-bose-429a601a0)
-   [Instagram](https://www.instagram.com/tapajyotib/)
-   [Upwork](https://www.upwork.com/o/profiles/users/~01c12e516ee1d35044/)