Git, the basics

TLDR: The idea of this post is to encapsulate the basic commands and concepts of GIT.

Why is so important to use a version control system?

Well, it’s kind of a difficult question to answer. But, it’s a good one that everyone made one day. My answer it’s, shit happens. Sometimes you will need to roll back some changes because a new feature is not working fine or just check what did you do one week ago?.

Version control is the best way to identify the exact changes made in the application. Keep track of the changes, collaborate with other developers keeping track of all the changes that the team made.

Version control Tools.

There are several version control tools available in the market. In more recent times, git has become the most popular one.

There are some other tools available:

  • TFS (Team Foundation Version Control)
  • SVN (Subversion)
  • Mercurial

What is Git?

Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

In summary, Git will provide you a complete copy of the entire codebase of your project on every contributor’s computer. Codebase also can be called local repository.

So, the main functionality of Git is to track all changes in the codebase inside a local database.

Git Snapshots

The git snapshot is the state of the project files at a given point in time.

So, instead of maintaining a full copy of the entire codebase, Git will only maintain a snapshot of the project files at a given point in time.

Git States

There are three main states that project’s files can be in:

  • Modified: The file has been modified. But that doesn’t mean that the file will be part of the next snapshot.
  • Staged: The file change has been tracked by Git and will be part of the next snapshot.
  • Committed: The staged files included in the previous step have been committed and are now ps part of the latest snapshot.

Git install and Config

Install Git and configure it. Verify the install is working.

$ git --version
git version 2.32.0

The config in git can work on different levels. But the main goal is to set the credencials that will identify the user changes. That can be locally or globally.

Setting the email and username for Git globally.

git config --global user.email "hi@example.com"
git config --global user.name "Educative Learner"

And verify the current config.

git config user.email
git config user.name

If they work fine, you should be able to see your email and username.

Create a new project with Git

So, when you want to create a new project, you need to create a new directory as you will see in the following command.

$ mkdir cats-project
$ cd cats-project
$ touch cats_list.txt

Inside the new directory, we can initialize the project with Git with the following command.

$ git init

The git init command simply creates an empty repository in the current directory. Internally, Git will create a directory called .git and inside that directory, and that directory will contain all the metadata that git will require for tracking the project.

git add command.

The git add command will help us to add files to the staging area. Git will track the content of the file and will keep track of the changes. So, enter the command in the current path.

➜  cats-project/ git:(master) $ git add .

The . is the current directory. But, if you want to track a specific file like cats_list.txt you can use the following command.

➜  cats-project/ git:(master) $ git add cats_list.txt

git status command.

The git status command will show the status of the project. It will show the files that are staged, modified, untracked, etc. That command doesn’t change or update anything.

➜  cats-project/ git:(master) ✗ git status
On branch master

No commits yet

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

nothing added to commit but untracked files present (use "git add" to track)
➜  cats-project/ git:(master) ✗ git add cats_list.txt
➜  cats-project/ git:(master) ✗ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
    new file:   cats_list.txt

➜  cats-project/ git:(master) ✗

The information that is shown in the git status command is very detailed. You can see the files that are untracked in the first block. Then, there is the git add command, and finally, you can see the files that are staged.

git commit command.

Right now, we have a local repository that is tracking our folder. But, we need to commit the changes that we made in the project. So, we need to use the git commit command.

A commit is a snapshot of the entire state of the project at that specific moment. The most recent snapshot of your repository is the HEAD. As soon as you create a new commit, the HEAD will point to the new commit.

➜ State 1
Commit 1 (HEAD -> master)

➜ State 2: After a second commit
Commit 2 (HEAD -> master) -> Commit 1 (master)

➜ State 3: After a third commit
Commit 3 (HEAD -> master) -> Commit 2 (master)

It’s mandatory to add a message to the commit. The message will be used to identify the commit. To do so, we can use the git commit -m 'Text to be included to describe the commit content' command with the -m flag and then the text. The idea of this text is to make sure it is sufficiently descriptive, and give a clear idea of what the commit is about.

The first commit of the project.

➜  cats-project/ git:(master) ✗ git commit -m 'Initial commit'
[master (root-commit) 033c02f] Initial Commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 cats_list.txt

➜  cats-project/ git:(master) git status
On branch master
nothing to commit, working tree clean

After the commit command is executed, the git status command will show the message nothing to commit, working tree clean.

git log command.

Basically, the git log command will show the history of commits performed in the project. If the project has multiple collaborators, you can find several commits and his authors.

➜  cats-project/ git:(master) git log
commit 033c02f2726c0d5e64ee09b97cbf50472d034f1c (HEAD -> master)
Author: Ramiro Andres Bedoya <hi@ramirobedoya.me >
Date:   Wed Sep 22 20:43:16 2021 -0500

    Initial Commit
(END)

You can press esc key to exit the command.

As you can see, each log will contain information about the author of the commit, the date of the commit, and the message of the commit. Another important piece of information is the unique hash of the commit. That hash helps to identify the commit. That hash uses the cryptographic hash function SHA-1.

git reset command.

The git reset command is a powerful tool to undo commits. For example, if you want to undo the last commit, you can use the following commands.

➜  cats-project/ git:(master) touch dogs_list.txt
➜  cats-project/ git:(master) ✗ git add dogs_list.txt
➜  cats-project/ git:(master) ✗ git commit -m 'dogs list'
[master 9588849] dogs list
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 dogs_list.txt
➜  cats-project/ git:(master) git status
On branch master
nothing to commit, working tree clean

I create a new file call dogs_list.txt and, I add it to the staging area. Then, I commit the changes.

The flag --soft

This flag is used to modify or update the changes from the previous commit without removing them completely. The complete command for the latest commit is git reset --soft HEAD~1. The HEAD~1 indicates how many commits you want to go back.

➜  cats-project/ git:(master) git reset --soft HEAD~1
➜  cats-project/ git:(master) ✗ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    new file:   dogs_list.txt

This flag changes the state of the committed files to staged.

The flag --hard

This flag is used to remove the complete changes from the previous commit. So, with the git reset --hard HEAD~1 command, the state will show the message nothing to commit, working tree clean.

Branches

I believe this is one of the most common causes. Let’s say that you are working in a special feature for the project. There is a log of changes but is still in progress. Let’s say that you need to fix a bug with a high priority. So, here is the best situation where the branches are used.

If you are working with branches, you can easily commit your changes to the current branch, then change into main and create a new branch. After that, you can fix the bug and merge those changes into the main. Finally, you can go back to the feature branch and resume the work.

The master branch

In Git, the master branch is the default branch created in a new repository.

All the commits executed in the previous example will be stored in the master branch. However, if you want to create a new branch, you can do it with the git branch command.

The idea with the branches is that you can create new branches that divert away from the master and you can continue to do your work from that new branch. The changes made in the new branch only will be reflected in this branch and won’t affect the master branch.

The task that Git does when a new branch is created is a new pointer to the current commit of the project. Therefore, the master branch has its pointer, and when you take a new branch out from the master branch, your new branch will have its pointer separate from the master.