As we all use git every day in our projects and it has made our lives easier by allowing us to collaborate with others more efficiently.Thus it helped us to produce great software that is impacting our lives every day.

What is Git?

In simplest terms Git is distributed version control system that allows us to keep track of our files and helps us collaborate with each other more efficiently while working on a software project. Git was created by Linus Torvalds in 2005 for development of the Linux kernel. It is primarily written in C and other languages such as python,shell, Perl and Tcl`.

Lets Begin Exploring Git

In order to begin I will start by creating a git repo named test/

  • git init test command creates a local repo named test on our machine that contains .git/ folder which contains all the metadata.

Image

In this blog, I will be focusing on objects and ref since they are the most important parts of .git/ directory.

How Git Actually works

Image

So I will be explaining how everything works under the hood using this simple diagram. As you can see we have three things working directory, staging area and local repo.

When we run git add it creates a snapshot of our file that we are working on and put that file in the staging area and then we run git commit command which saves that snapshot of our file permanently in our local repo.

What happens when we run git add <filename>

add.png

When we run git add command it takes the files from the staging area and saves the file as a copy under objects directory. The file is represented as a blob (Binary Large Object) and that name of the file is generated using SHA-1 hashing algorithm as it was calculated using the contents of the file.

In this case it is represented by f9cae9b and the object can be seen by using the following command git show --pretty=raw f9cae9b.

What happens when we run git commit <filename>

When we run git commit it takes our file from staging and saves as a permanent snapshot in our local repo. When we run git commit command two more objects are created. A tree & A commit

As you can tree is represented by 1be1fd

tree.png

and commit is represented fcae310. furthermore, if you notice it also has a tree and it represents our directory structure.

commit.png

If we run git ls-tree <tree> it prompts more us information about our tree and what type of files it contains.

tree-info.png

So we have a file named hello.txt in our tree which is represented as a blob object which it refers to f9cae9b. moreover, file permissions are also tracked by git which is represented as 100644 in this case. Git is so intelligent that when we change our file name our raw data isn’t deleted but a new tree is created in our object, therefore, it allows us to change file names easily without losing raw data.

Besides, objects/ directory we also have refs/ and this contains our branches. Branch name acts a reference to the commit. It contains a SHA that is pointing to our commit and it is automatically updated whenever we make a change and commit our file to the repo.

So this is how Git works under the hood. Furthermore, Git uses a data structure called Directed Acyclic Graph and it’s really interesting to see how git works and how it has made collaboration easier for developers.

I hope you enjoyed reading this post and if you think I missed anything feel free to comment below. I will be writing more about git rebase and git checkout commands and how they work in another post.

Feel to share it among your friends and colleagues.

I will be writing at least 4 articles every month on programming/interesting topics. Feel Free to Subscribe