Skip to main content

Command Palette

Search for a command to run...

Inside Git: How it works and role of the .git Folder

Published
3 min read

Have you ever noticed that hidden .git folder in your project and wondered why it’s there? Or why Git is so fast and reliable?

So, basically the folder stores all the information related to the repository of your project and it is the reason git is so fast because it operates offline within your computer.

Understanding the ‘.git’ folder

Whenever you initialize a new git repository using the git init command, it simply creates a .git folder within the project folder (‘.git’ is hidden folder). This folder is basically the storage system of the git repository where all the things related to it. Every commit you perform is saved here and that is one of those reasons you are easily able to access your old code.

Key contents of .git:

  • /objects: Every version of file is stored here.

  • /refs: Pointers to commits (like where main or head is).

  • config: Your project-specific settings.

Git Objects: Blob , Tree and Commit

Git doesn’t store your whole codebase, it just takes the snapshots at the point where you want and it uses these 3 objects to store it and connect all of them together.

  • Blob (Binary Large Object):

    It stores the content of your file.

    It doesn’t filename or date, it just stores the data. So if two files have same text it stores a single blob.

  • Tree:

    This object acts like a folder. It maps the filename to the respective blob.

  • Commit:

    The snapshot of the whole project, it points to a single tree and contains metadata like author, date and pointer to the previous commit.

How Git tracks changes ?

Now, how does git track changes ? Well it uses Hash, git runs your file content through an algorithm to generate a unique 40-character string. If you change a character in a file the hash changes, this also keeps in keeping the integrity of the code.

Now lets know what happens internally when we run commands like git add and git commit.

  1. git add:

    • This command sends the file to staging area.

    • Here, git reads the file then creates a blob in the /objects folder.

    • It updates the staging area to track that the blob is ready to be part of next snapshot.

  2. git commit:

    • This command saves the snapshot.

    • Git creates a Tree object representing the current folder structure.

    • Git creates a Commit object that points to that Tree.

    • And finally moves the branch pointer to a new Hash.

Thank You

With this, the blog ends and now you know what really happens when we run the git commands and how it tracks changes and stores, tracks changes.

More from this blog

T

TheRealShreyash

16 posts