DEV Community

Cover image for Git Simplified: Fundamental Concepts & Commands
Tapajyoti Bose
Tapajyoti Bose

Posted on • Updated on

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:

Without Git

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:

With Git

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
Enter fullscreen mode Exit fullscreen mode

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

git --version
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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> <...>
Enter fullscreen mode Exit fullscreen mode

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

git add .
Enter fullscreen mode Exit fullscreen mode

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)>"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Logs

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
Enter fullscreen mode Exit fullscreen mode

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.

Git Hash

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

git revert 8a11c5095f2dcd70b0bc8c66061a1368558a3abf
Enter fullscreen mode Exit fullscreen mode

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

An additional commit is added on reverting modifications

Git Revert Commit

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

To create a new branch use:

git checkout -b <new branch name>
Enter fullscreen mode Exit fullscreen mode

To switch to an existing branch use:

git checkout <branch name>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Git Merge

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

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

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

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:

Oldest comments (1)

Collapse
 
bobbyiliev profile image
Bobby Iliev

Great article!

For anyone interested in learning more about Git and GitHub, I could suggest this open-source eBook here:

GitHub logo bobbyiliev / introduction-to-git-and-github-ebook

Free Introduction to Git and GitHub eBook

💡 Introduction to Git and GitHub

This is an open-source introduction to Git and GitHub guide that will help you learn the basics of version control and start using Git for your SysOps, DevOps, and Dev projects. No matter if you are a DevOps/SysOps engineer, developer, or just a Linux enthusiast, you can use Git to track your code changes and collaborate with other members of your team or open source maintainers.

The guide is suitable for anyone working as a developer, system administrator, or a DevOps engineer and wants to learn the basics of Git, GitHub and version control in general.

🚀 Download

To download a copy of the ebook use one of the following links:

📘 Chapters

  • About the book
  • Introduction to Git
  • Version Control
  • Installing Git
  • Basic Shell Commands
  • Git Configuration
  • Introduction to GitHub
  • Initializing a Git project
  • Git Status
  • Git Add
  • Git