Understanding Git

I believe this is an important post where we will discuss about the internals of Git and how it works. Understand the topics in this post will serve as a solid base for the rest of the posts.

The topics that will be discussed are as follows:

  1. Git snapshots
  2. Most Git operations are local
  3. Integrity in Git
  4. Git states

1. Git snapshots

Git, unlike other VCS stores snapshots of the asset at the time of saving your work.

Snapshot

Asset Snapshots

We will try and understand the concept of Git snapshots using the above diagram.

  • Assume that the asset is initially composed of three files (file1, file2 and file3).
  • Snapshot, as the name suggests is a picture of how the asset looks during every time work is saved.
  • Initially, Snapshot1 = File1 V1 + File2 V1 + File3 V1
  • Now lets say you make some changes to File2, thereby creating a new snapshot in Git. Snapshot2 = File1 V1 + File2 V2 + File3 V1
  • Continuing on, lets say you make changes to File1, File2 and File3. Additionally, you add a new file (File4) to the asset. Thereby Snapshot3 = File1 V2 + File2 V3 + File3 V2 + File4 V1

2. Most Git operations are local

From the earlier post, you already know that every Git client stores the asset in local disk. In this post, we have seen how Git stores snapshots of every version.

Therefore, Git stores all the snapshots in local of every client. This allows client to work locally for most of the operations. It only need to connect to remote server only when they have to save their work for others to see.

3. Integrity in Git

Every operation in Git is checksummed before it is stored and is referred to by that checksum.The mechanism that Git uses for this checksumming is called a SHA-1 hash. This is a 40-character string composed of hexadecimal characters (0–9 and a–f) and calculated based on the contents of a file or directory structure in Git.

4. Git States

A file in git can reside in one of following three states:

  1. Working Directory – It is the place where you make your changes (Files are said to be in modified state here).
  2. Staging Area – It is the place where you add the changes that you made in the working directory and get them ready for commit (Files are said staged here).
  3. Git Repository – It is the place where you commit the changes that you previously added in the staged area. (Files are said to committed here).

However, the changes are still not visible to everyone. The changes are present in your local repository (which was a copy of the central repository). Now you have to make a push, to save it on the central server for everyone to see.

2.2

Git States

Since now we know a little about how Git works internally, we should be good to move forward. Before moving on to the commands, I would like us to get comfortable in the language of Git.

The next post will be for getting you familiarized with Git terminology.

References – https://git-scm.com/book/en/v2/Getting-Started-Git-Basics

Tagged: ,

Leave a comment