In this blog, I will be focusing on
ref since they are the most important parts of
So I will be explaining how everything works under the hood using this simple diagram. As you can see we have three things
staging area and
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.
git add <filename>
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.
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
and commit is represented
fcae310. furthermore, if you notice it also has a tree and it represents our directory structure.
If we run
git ls-tree <tree> it prompts more us information about our tree and what type of files it contains.
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.
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