if you reading this means I will assume you know what exactly git is for!! , We will not talk about how push or commit here we will talk about what happens when you commit or push the code.
All of this starts from the command git init which initializes the repo gets the .git file ready to use.
Once you have committed the first commit , you will see a .git folder in the working directory , Before knowing what is inside it let's first understand what it is and how it operates. Think of .git as the database storing the entire history of commits along with the data(code) in form of objects it used to retrieve and store data pretty differently than previous Version Control Systems(VCS) which follow traditional method of storing the entire file structure that has been changed unlike other VCSs , Git helps to store data pretty efficiently and effectively and reduces space significantly.
So what happens inside that when you get inside the .git folder you see multiple folders inside it
As you see multiple folders inside the .git folder each have it's own work but for now let's focus on the objects/ folder when you get inside that you will see 3 folders since you have committed the first time and two folders named info/ and pack/.
Ignore this two let's focus on other folders , each of the folders named a numbers like 01/ 02/ 03/ or anything else. Now we comeback to our terminal check that when you run git log.
This will log the commit you did with a random hash value(gibberish mixture of letters and numbers) , and meta data about your commit (who did it and when) as you can see on the image.
Now you may want to notice that the first two digits of the hashvalue is the name of one of the folders inside the .git/objects/ directory if you open that folder you will see the rest of the hashvalue as a file stored there
Ex: complete hashvalue : 4834726f1da14527eb539cf75be80db8f6f286ae
Now look at it this way :48 / 34726f1da14527eb539cf75be80db8f6f286ae
Now what is this file and what is it doing here
ok before that let me give you some information about what exactly is stored in this objects folder as you saw it contains multiple folders if we ignore those other 2(info/ , pack/)
What are these folders?
So what happens is . Git divides your commits into three parts
- Commit : contains the metadata about the commit made including all the info of who and when also a hashvalue which redirects to tree folder.
- Tree : contains the directory structure at that point of time when committed and may or maynot connects to another trees which contains the blobs of the previous versions with no change in code.
- Blob: This actually stores the code snippets (the changes you made the actual data and lines of code) which is connected from the tree to get those version of data These are the 3 pillars of .git which help it work as database for your commits.
Now, the 48 is the commit folder that we know what is the hash value given if you check inside using the git cat-file hashid you will see it will show the metadata and another hashvalue with title tree this hashvalue points to the tree folder and the hashvalue named file similar to what we gave example for commit (48 / awdada ) , if you peak into it you will see that it is pointing to some other directories it can either be a blog or another tree containing different blogs it is like tree hierarchy structure each commit put on top of other connecting each other when required or creating blobs if necessary
Now let's think it redirects to a blob let's explore that
blob is also found in similar method filename/hashvalue
If you peak into it each blob contains some gibberish stuff which actually are the changes made each time in each file this is where the code snippets live and are hashed using SHA-1 algo.
And that is how the git commit —>store works
What happens when we retrieve a previous version or commit , when we do git checkout #hashid of the commit what git does is the thing first it retrieves the commit file from the objects folder then retrieves the tree structure with the hashvalue present at that file and once git fetches all the tree files and blobs it reflects it to the working directory of the user pretty simple right ;|
This is what my understanding .git folder is its amazing how it is storing data and the way of retrieving it
if you like this you might like to read this too What is git?






Top comments (0)