01 Git: Basics
Tags: git, github
Published: 2021-01-28 00:20:48

### What is Git and why has it become a must in the current software development world?

[Git](https://git-scm.com) is an open-source, distributed version control system created initially by [Linus Torvalds](https://github.com/torvalds), creator of [Linux](https://en.wikipedia.org/wiki/Linux) (& Git). You might be wondering what is a _[Version Control (or Source Control) System](https://en.wikipedia.org/wiki/Version_control)._ Well, in the world of software development today, versioning the software has become essential as it helps developers to keep track of new changes being done to the software and allows developers to collaborate on it. Also if something goes wrong, they can roll-back to the last stable version.

Knowing the purpose of Git, we can move on to how to use Git to build awesome pieces of software. The official installation guide is [here](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git). To use Git, first of all, we have to open a terminal and change our present working directory(aka pwd) to the directory where we want to start using Git. In Unix/Linux based systems, command `cd` will do what we want.

```
cd ~/path/to/directory
```

Once we're inside of target directory, we have to initialize the Git repo. We can do so by running the following in the terminal.

```
git init
```

Here is output of `git init` on successful execution:

```
Initialized empty Git repository in ~/path/to/directory
```

We discussed earlier that Git keeps track of changes in files of our project. From "tracking", there are two states of files inside a Git initialized directory. One is **tracked** by Git and the other, as you might've guessed, is **untracked**. To understand the concept of tracked and untracked files, we must have some files first. If the directory is empty, we can create a file and add some text to it directly by using `echo` command on Unix/Linux-based systems.

```
echo "1\. Learning Git and Github" > ./learnings.md
```

The `>` creates a new file named `learnings.me` and puts the output of `echo` in that file. Now as soon as we create this file, we can execute `git status` in the terminal to know the status of our tracked/untracked files.

<figure >
![Output of git status](https://dev-to-uploads.s3.amazonaws.com/i/xjh6mpkoq7s0qywqtbrr.jpg)
<figcaption>Output of `git status`</figcaption>
</figure>

Ignoring all other information, when we set an eye on the section of _Untracked Files_, we see that there is our file named `learnings.md`. I.e. Git recognized the fact that the file is new and there has never been a version of this file stored inside of Git and that makes the file **untracked**.

### Command: Git Add

Once the file is on the list of Untracked Files, we much move the files to the Git state of _Staged Files,_ where Git lists files that we changed the content of to update our software. In our case, we created a new file (`learnings.md`), which will be added to the list of _Staged Files_.

```
git add learnings.md
```

As discussed earlier, to know the status of Git tracked/untracked files, we run `git status`. Here is what Git will show us now once we've run `git add learnings.md`.

<figure class="kg-card kg-image-card">![](https://webdevjeet.me/content/images/2020/12/image-3.jpg)</figure>

### Command: Git Commit

Now we can see that our file is listed under the _Changes to be committed(aka staged file-state)_ section. Once files are staged(ready to be committed), we can tell Git to take a snapshot of the current state of files with `git commit`. In general, we provide a meaningful message(aka Commit-Message) with every commit, so that we can look back at commits later and recognize the changes done in a specific commit. We provide our Commit-Message with an `-m` flag with `git commit`.

```
git commit -m "added list of techs in learnings.md"
```

<figure>![Output of git commit -m "added list of techs in learnings.md"](https://dev-to-uploads.s3.amazonaws.com/i/m66c63a8pckhuijqnvlh.jpg)

<figcaption>Output of `git commit -m "added list of techs in learnings.md"`</figcaption>

</figure>

Git has taken a snapshot of current files in the directory and since this is the first commit, it is labeled as "root-commit". We discussed earlier that the main application of Git is to keep track of changes in our files. So now it's time to change the content of the file we created earlier. In Unix/Linux systems, we can use `>>` with `echo` to append the content at the end of a file making (more info on `>>` [here](https://superuser.com/questions/1334939/what-does-mean-in-terminal-command)) a _change_ to the file and that's where Git kicks in.

```
echo "2\. ReactJS and VueJS" >> learnings.md
```

We can use `cat` command to print the content of a file in the terminal in Unix/Linux systems. And we see that the file's been updated with a new line containing "2\. ReactJS and VueJS".

<figure>![](https://dev-to-uploads.s3.amazonaws.com/i/fuf0xwrbhdokfwswegse.jpg)</figure>

### Command: Git Diff

Now that we have an updated file that we once committed into Git earlier, we can ask Git to show us differences between the earlier committed(saved) version and the current updated file with the command `git diff`.

<figure>![Output of git diff](https://dev-to-uploads.s3.amazonaws.com/i/t7j7ym4sblqus7idagpj.jpg)

<figcaption>Output of `git diff`</figcaption>

</figure>

Git diff shows us a green line with a "+" at the start. It stands for the fact that the content that was added to the file is new. Once again we can commit these changes to make a snapshot of the current state of git. Running `git add learnings.md` and `git commit -m "added ReactJS and VueJS to the learnings list"` subsequently would stage and commit our new change in the file. We can see the list of commits by executing `git log`.

<figure>![Output of git log](https://dev-to-uploads.s3.amazonaws.com/i/pfa60kbqjyf0849tl4hq.jpg)

<figcaption>Output of `git log`</figcaption>

</figure>

If we wish to be informed about the Commit-Messages only, we can use `--oneline` with `git log`.

<figure>![Output of git log --oneline](https://dev-to-uploads.s3.amazonaws.com/i/v8x5uzj6ivfegyyl9kc5.jpg)

<figcaption>Output of `git log --oneline`</figcaption>

</figure>

### Quick Revision Of Topics Discussed Till Now:

*   initializing git in a directory with `git init`
*   staging files with `git add <File Name>`  
*   committing them with `git commit -m "meaningful-commit-message"`
*   see the difference between the current state of files and the last committed version of files with `git diff`
*   see the information about the commits with `git log` or `git log --oneline`(for compact info)

**Hurray 👏 👏 👏 , now you know how git operations that developers do 1000s of time in a typical year are done!!! 🥳**

Next post in this series is [02 Git Remotes and GitHub](https://webdevjeet.me/2020-12-git-remotes-and-github/) where we go deep into pushing repos to GitHub and manage remotes to local git repo.

---

This post is originally written [on my blog.](https://webdevjeet.me/2020-12-git-basics/)