Git Simplified: Fundamental Concepts & Commands

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:

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) ends up looking condensed like this:

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'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:

sudo apt-get install git

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

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:

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:

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

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

git add .

3. Committing Changes

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

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:

git log

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 typess 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.

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

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.

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>

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.

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

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.

Thanks for reading

Want to work together? Contact me on Upwork

Want to see what I am working on? Check out my GitHub

I am a freelancer who will start off as a Digital Nomad in mid-2022. Want to catch the journey? Follow me on Instagram

Follow my blogs for weekly new tidbits on Dev

Connect to me on:

27