Skip to main content

Command Palette

Search for a command to run...

About .git folder and it's existence

Updated
5 min read

How Git works internally

Here we will learn about the working of git. It is a hidden folder which is created by the Git repo when we run a command ‘git init’ . It creates this folder to track the changes, without this folder our project is not a repository. This folder is the core of the git repository .

Git track what happening in this folder and we are only who give the permission to track the changes. The first ever command we’ll be using is the “git —version” when we run this command we get the version of the Git which we have installed in our local system basically to check the version of the Git which we have installed in our local system .

Understanding the ‘.git’ folder

This ‘.git’ a hidden folder is like a database of the project tracking. It contain all the essential thing which helps in tracking the changes in our project. The thing which is stored in this folder basically the things we have changed in our project like deletion , addition , and when we have done all this changes.

It store the folders like :-

  • objects

  • refs

  • Head

  • config

  • index

  • logs

Git Objects: Blob, Tree, Commit

We do the regular basic thing in our projects are write and then add it and the commit. Git stores everything like a object, it creates three main objects and they are blob, tree and commit.

Blob

Blob is the actual file or a code which needs to be saved. It stores all content of the files only.

Like if we write

fileOne.html

/* Having the message */
'Hello World'

/* The Blob conatins only the message we have written in the fileOne.html*/

Tree

Now, the tree just by hearing the word tree there is a image of a tree where we imagine a tree have branches and the structure like starting from the single lane to a multiple routes (spreading). This is the same thing tree does here. It gives us the structure of the project’s files and folders with its name. And it is the reference of the Blob .

We have this structure which we see

project/
 ├── fileOne.html
 └── src/
     └── app.js

But the things goes in it like

Tree (project)
 ├── fileOne.html => Blob
 └── src → Tree
      └── app.js => Blob

Commit

Commits are just like the screenshots of the latest or updated project . Here we can assume like the top of the tree. And it takes the reference of the Tree . Few things which commits contain are :-

  • Parent commit ( may have multiple parent commit )

  • Commit message

  • Time Stamp

  • Authors information

And this is the thing goes on in the Commit object is

Commit A
 ├── Tree (root)
 ├── Parent => Commit B
 ├── Author: You
 └── Message: "Initial commit"

How Git Tracks Change

Git track changes by three main things :-

  1. Content of the file

  2. State of the file

  3. History of the changes

Content of the file

Whenever we do a commit a SHA-1 hash get created and Git track changes just by comparing the SHA-1 hash. Git just compare the last commit and the SHA-1 hash if the hash changes that means you have changed something, even a single character is getting tracked by the SHA-1 hash. Git recalculate the hash which is generally of 40 characters if we do any changes it change the 40 character hash. And when Git recalculate it get the different hash then Git mark it as modified which means you have done few or more changes in the file if it gets the same hash then it mark as unchanged.

State of the file

It track the files status that what is the status of the file untracked, modified, staged or committed.

Untracked if the file exists in our folder but Git is unaware of it that means it is untracked. But if the file is marked as modifies it clearly means that there are few changes have been done by the user after the file was committed. But if we have added the file by running this code

git add index.html

Now, our code have been added to staging are just like taking a screenshot of it. And it is ready for commit . The code get saved permanently by writing this code

git commit -m "Here the code is being permanently saved"

After running this code our code will be uploaded or saved permanently to our server which we are using likely GitHub or GitLab.

History of the changes

Git store or maintain the history of the commits like chain structure. Whenever we do a commit the commit stores/save our details of author details , commit time , messages , parent commit and structure of the project ( Tree ) and each of this is being saved in the commit. Each commit know its parents because it is stored in history.

When we do commit it save the file blobs, creates tree, create commit object and store parent commit hash and Git use our old blobs if our file’s content is not changes otherwise it creates a new blob.