Skip to main content

Command Palette

Search for a command to run...

How It Works and the Role of the .git Folder Inside Git

.git is the hidden folder that saves your work

Updated
2 min read

Understanding the .git Folder

When you initialize a Git repository, a hidden folder named .git is created. This folder is the main of your repository. It contains all the necessary information to track changes, including your project's history, configuration settings, and pointers to the current state of your code.

As shown in the image below, the most important components are:

  • objects/: This is Git's database. It stores all your file contents, directory structures, and commit history as unique objects.

  • refs/: This directory contains pointers (references) to the tips of your branches and tags.

  • HEAD: A special file that points to the current branch reference, telling Git "where you are" right now.

  • index: A binary file that serves as your staging area.

Git Objects: Blob, Tree, Commit

Inside the objects/ folder, Git stores its data in three primary object types, as illustrated below:

  • Blob (Binary Large Object): Stores the content of a file. It does not contain the filename or permissions.

  • Tree: Represents a directory. It contains a list of filenames and pointers to their corresponding blob or other tree objects.

  • Commit: A snapshot of your project at a specific point in time. It points to a root tree object and includes metadata like the author, date, and a link to its parent commit(s).

How Git Tracks Changes:

Git does not track changes line by line internally. Git records a complete snapshot of the project at that moment.

Git stores everything as objects in .git/objects.

  • Git stores blobs (full content).

  • When you run git diff, Git compares two blobs and calculates the line differences on the fly.

This is better :

  1. Fast checkout of any commit

  2. No need to apply chains of patches

When you run git commit, Git uses the information in the Index to create a new Tree object, which represents the state of your project's root directory. Then, it creates a new Commit object that points to this new Tree. This new Commit also links to its parent commit. Finally, the HEAD pointer is updated to point to this new Commit, effectively advancing your branch.