Skip to main content

Command Palette

Search for a command to run...

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

Published
3 min read

How Git Works Internally?

  • Git does not track changes like a traditional “file editor.” Instead, it stores snapshots of your project over time. Every time you commit, Git saves the exact state of your files and links it to previous snapshots.

Understanding the .git Folder

  • The .git folder is the heart of a Git repository. It is created when you run git init.

  • The .git folder contains the entire commit history, all Git objects such as files, folders, and commits, along with branch information and configuration data; if this folder is deleted, the project becomes a normal folder and all version history is permanently lost, which is why the .git directory can be thought of as Git’s database where everything is stored and managed.

Git Objects: Blob, Tree, Commit

  1. Blob (Binary Large Object)

    Git stores only the contents of files, not their filenames or folder structure; it saves this data as blobs, where each blob represents file data, and if two different files have identical content, Git stores just one blob and reuses it for both.

  2. Tree

    A tree represents a directory in Git; it stores file names and references that link to blobs (file contents) or other trees, effectively capturing the folder structure.

  3. Commit

    A commit is a reference to a tree that represents a snapshot of the project at a point in time, includes a reference to the previous commit, and stores metadata such as the author, date, and commit message.

What Happens Internally During git add

When you run git add:

  1. Git reads the file content

  2. Creates a blob object

  3. Stores it inside the .git folder

  4. Adds a reference to the staging area

Think of staging as telling Git what you want in the next snapshot.


What Happens Internally During git commit

When you run git commit:

  1. Git creates a tree object from staged files

  2. Git creates a commit object

  3. The commit links to:

    • The tree (current snapshot)

    • The previous commit

  4. HEAD moves to the new commit

How Git Tracks Changes

Git does not store differences (diffs); instead, it stores complete snapshots efficiently by reusing existing blobs for unchanged files and creating new objects only for new or modified content, which makes Git fast and space-efficient.

How Git Uses Hashes for Integrity

Every Git object is identified by a SHA-1 hash (e.g., a3f5c9...).

Git uses hashes to:

  • Identify objects uniquely

  • Detect file corruption

  • Prevent accidental changes

If file content changes, the hash changes — guaranteeing data integrity.

Mental Model to Remember Git

Think of Git as:

  • A content-addressed database

  • Storing snapshots, not edits

  • Using hashes as permanent IDs

  • Linking commits like a chain