DEV Community

Cover image for What's Inside the .git Folder?
Satpalsinh Rana
Satpalsinh Rana

Posted on • Originally published at satpalsinhrana.hashnode.dev

What's Inside the .git Folder?

In this article, we are going to open the hood and look at the engine. We are going to explore the .git folder.

By the end of this article, you’ll understand how Git stores files, tracks history internally, and how commits are built under the hood.

Prerequisites

Before we dive into the hexadecimal rabbit hole, let’s make sure we are on the same page. You don’t need to be a Git wizard to follow along, but you should be comfortable with:

  • Basic Git Commands: You have used git add, git commit, and git push before.

  • The Command Line: You know how to open a terminal and navigate folders (cd, ls).

  • Curiosity: You are willing to look at things that might seem abstract at first!

New to Git?

If you want to refresh the basics first, check out:

Learn Git Easily: A Beginner's Guide to Version Control

What is the .git Folder ?

If you open any Git repository on your computer and enable "show hidden files," you will see a folder named .git .

This isn't just a config folder, it is a database. It is the brain, heart, and soul of your repository. If you delete your project files but keep the .git folder, you can restore everything. If you delete the .git folder, your project becomes just a regular folder of text files, and all your history is lost.

Git is essentially a content-addressable filesystem. That’s a fancy way of saying it’s a key-value store.

  • Blob: When you create a file like main.ts , Git looks at the contents of that file. It compresses the content and stores it as a Blob.

  • Tree: A blob has content, but it doesn't have a name. That's where the Tree comes in. It maps names (like main.ts) to Blob IDs. It can also contain other Trees (subdirectories). This is how Git reconstructs your folder structure.

  • Commit: The Commit object is the wrapper that ties it all together. A pointer to a specific Tree, metadata, and a pointer to the parent commit.

In Simple terms:

  • Blob: The data (File content).

  • Tree: The structure (Folder organization).

  • Commit: The snapshot (Who, when, and why).

SHA-1 Hashes

You’ve likely seen those long strings of random characters like a1b2c3d... . This are SHA-1 Hashes.

Every time you create an object, Git runs a mathematical formula on the content to generate this 40-character ID.

Here, SHA stands for Secure Hash Algorithm.

Newer versions of Git now support SHA-256 as an option, but it is not the default yet because the entire ecosystem (GitHub, GitLab, tools) is still built around SHA-1 compatibility.

To check which hashing algorithm your specific Git repository is using, you can run this command in your terminal inside the repo:

git rev-parse --show-object-format
# rev-parse - Git plumbing command that resolves and parses Git references (hashes, branches, tags)
# --show-object-format - Displays the object hash format used by this repository (sha1 or sha256)
Enter fullscreen mode Exit fullscreen mode

Command Result

What happens when you commit?

Let’s trace the journey of a file from your editor to the repository.

Step 1: git add .

When you run git add, you aren't just "marking" files. You are actively writing to the database.

  1. Git takes the content of your modified files.

  2. It creates Blob objects for them immediately and stores them in .git/objects.

  3. It updates the Index (or Staging Area). The Index is just a list that says, "For the next commit, this filename points to this specific Blob hash."

Step 2: git commit -m "Message"

When you run git commit, Git creates a permanent snapshot.

  1. Tree Creation: Git looks at your Index (Staging Area) and creates Tree objects that represent your current folder structure.

  2. Commit Creation: Git creates a Commit object. This object points to the main Tree you just created and links back to the previous commit (the parent).

  3. Head Update: Finally, Git moves the HEAD pointer (your current branch label) to point to this new commit ID.

So, this is where the integrity magic happens.

  • If you have a file with the text "Hello World", the hash will always be the same.

  • If you change "Hello World" to "Hello Worlds", the hash changes completely.

Git hashes not only the content, but also the object type and size. This guarantees uniqueness and integrity. Git objects don't just exist in isolation, they point to each other using these hashes. This dependency creates a chain reaction. This happens because every commit also includes the hash of its parent commit.

Deep Dive: Folder Structure

.git Folder structure

HEAD (The "You Are Here" Pointer)

The HEAD file is a simple text file that tells Git where you are currently working. It contains a reference to the active branch.

If you open it, you will typically see:

ref: refs/heads/main
Enter fullscreen mode Exit fullscreen mode

This tells Git: "We are currently on the main branch." When you run git checkout dark-mode, Git simply updates this file to point to refs/heads/dark-mode. It is essentially a dynamic pointer.

objects/ (The Database)

This is the most important folder. It acts as the local database where Git stores all your data—content (blobs), folder structures (trees), and history (commits).

Every object is stored here, named by its unique SHA-1 hash.

How to inspect an object: Since these files are compressed binaries, you cannot open them in a text editor. Instead, use the git cat-file command to read them.

git cat-file -p <object-id>
Enter fullscreen mode Exit fullscreen mode
  • -p: Pretty-print the content (make it human-readable).

Example Output of a Commit Object:

tree 8d3a1...
parent 1f4e2...
author Satpal Rana <satpal@example.com> 1703865000 +0000

Created the homepage hero section
Enter fullscreen mode Exit fullscreen mode

refs/ (The Bookmarks)

If objects/ is the library of books, refs/ is the catalog of bookmarks. This folder stores your Branches and Tags.

  • Heads: Found in refs/heads/. If you have a branch named feature-login, there is a file named feature-login in this folder.

  • The content: The file contains nothing but a 40-character Commit Hash.

This is why creating branches in Git is so fast. Git doesn't copy your files, it just creates a tiny text file containing the hash of the current commit.

index (The Staging Area)

This is a binary file that acts as the "Staging Area." When you run git add, Git updates this index file. It builds a list of file information (timestamps, filenames, and blob hashes) that are ready to be packaged into the next commit.

config (Project Settings)

This text file contains configuration specific to this repository, such as:

  • The URL of the remote repository (origin).

  • Local user overrides.

  • Branch tracking information.

Hand’s On Git

  1. Create a new repository:
git init demo
cd demo
Enter fullscreen mode Exit fullscreen mode
  1. Create a file and commit it:
echo "Hello Git" > hello.txt
git add .
git commit -m "First commit"
Enter fullscreen mode Exit fullscreen mode
  1. Explore the objects:
ls .git/objects
git cat-file -p <object-id>
Enter fullscreen mode Exit fullscreen mode

Observe how Git stores and retrieves data internally.

In this article, we explored how Git stores data internally using blobs, trees, commits, and hashes. Understanding this makes Git feel less magical and more predictable.

“Hidden folders sometimes hold the biggest secrets.”

— Anonymous Developer

Top comments (0)