Git : The Basics

Git : The Basics

Understanding the basics of Git and Installing Git on your machine

In the world of software development, version control systems are an indispensable tool for managing code changes, collaborating with others, and keeping track of project history. In the previous post of this series, we discussed the importance of version control systems, and their types, and introduced some essential terminologies. In this post, we will focus on one of the most popular version control systems, Git, and its basics. Understanding the fundamentals of Git can help you streamline your workflow, avoid common pitfalls, and collaborate more efficiently with your team. So, let's dive in!

About Git

Git is a distributed version control system that was created by Linus Torvalds in 2005. It's incredibly popular due to its flexibility, speed, and ability to handle large codebases. With Git, developers can work on the same codebase simultaneously, and easily switch between branches to work on different features or fixes. Git also provides a wide range of tools to help with collaboration, such as merge requests and pull requests.

Snapshots, Not Differences

Git is different from other VCS because it thinks of its data like a series of snapshots of a miniature filesystem rather than a list of file-based changes. Git stores data as snapshots of the project over time.

Nearly Every Operation Is Local

In Git, nearly every operation is local because most operations only require local files and resources to operate. This means that it is very little you cannot do if you are offline.

Git has Integrity

Git has integrity because everything in Git is checksummed before it is stored and is then referred to by that checksum. Git uses SHA-1 hash to store everything in its database not by file name but by the hash value of its contents.

Git Generally Only Adds Data

When you do actions in Git, nearly all of them only add data to the Git database. It is hard to get the system to do anything that is not undoable or to make it erase data in any way. After you commit a snapshot into Git, it is very difficult to lose, especially if you regularly push your database to another repository.

The Three States

Working tree, staging area, and Git directory

Git has three main states that your files can reside: modified, staged, and committed.

  • Modified means that you have changed the file but have not committed it to your database yet.

  • Staged means that you have marked a modified file in its current version to go into your next commit snapshot.

  • Committed means that the data is stored in your local database.

In simple terms, Git has three main parts: the working tree, the staging area, and the Git directory.

  • The working tree is the current version of the project that you are working on. It contains all the files you need to use or modify.

  • The staging area is a file that contains information about what changes you want to include in your next commit. You can selectively stage changes, which means you can choose which changes you want to commit and which ones you don't.

  • The Git directory is where Git stores all the information about your project, including the history of changes and the current state of the project. When you clone a repository from another computer, you are copying the Git directory.

The basic Git workflow involves making changes to your working tree, selectively staging the changes you want to include in your next commit, and then committing those changes to the Git directory.

Getting Started

Enough with the boring stuff! Now that we've covered the basics of Git, it's time to take the next step and start using Git.

Installing Git

Linux

The easiest way to install Git on your system is through the command line. Let's open a terminal. If you are working on Ubuntu or Debian, all you need to do is run the following command.

apt-get install git

For Ubuntu, this PPA provides the latest stable upstream Git version

add-apt-repository ppa:git-core/ppa
apt update
apt install git

If you are using a different Linux distro, you can find instructions on the Official Downloads Page.

Windows

To install git on windows, all you need to do is follow these steps.

  1. Go to the Git website at git-scm.com/download/win

  2. Click the download button for the 64-bit Git for Windows Setup

  3. Once the download is complete, double-click on the downloaded file to start the installation process

  4. Select the desired language for the installer and click 'OK'

  5. Follow the on-screen instructions to complete the installation

To verify the installation, you can do any of the following:

  • Go to any folder or the desktop and right-click, if you see "Git Bash Here", then you successfully installed Git on your machine.

  • Open a Command Line or Powershell, and type:

      git --version
    

If you see something like this: git version 2.39.1.windows.1, then you successfully installed Git on your machine.

Mac OS

There are several ways to install Git on a Mac. The easiest is probably to install the Xcode Command Line Tools. On Mavericks (10.9) or above you can do this simply by trying to run git from the Terminal the very first time.

git --version

If you don’t have it installed already, it will prompt you to install it.

Or you can follow the following steps:

  1. Go to the Git website at git-scm.com/download/mac

  2. Click the download button for the macOS installer

  3. Once the download is complete, double-click on the downloaded file to start the installation process

  4. Follow the on-screen instructions to complete the installation

  5. Open the Terminal app and type git --version in the terminal window and press enter to verify that Git is installed correctly. If you see the Git version information, it means Git has been successfully installed.

That's it! You now have Git installed on your machine and you're ready to start using it.

Conclusion

In this post, We've explained what Git is, how it works, and its advantages. We've also explored the three main states your files can reside in and provided installation guides for Linux, Windows, and macOS. I hope this post has been helpful to you, and I welcome your feedback in the comments. In the next post, we will dive deeper into Git's basic commands and help you create your first repository. So, stay tuned!

References