DevOpsGitHub

Git Internals: Objects, Blobs, and the .git Folder

TT
TopicTrick Team
Git Internals: Objects, Blobs, and the .git Folder

Git Internals: Objects, Blobs, and the .git Folder


1. The Four Horsemen of Git Objects

Everything in the .git/objects folder is one of these four:

  1. Blobs: Binary Large Objects. These store the Content of your files. A Blob doesn't know its filename; it only knows its data.
  2. Trees: These are like folders. They link Filenames to Blobs.
  3. Commits: These point to a Tree (The "Snapshot") and include a pointer to the "Parent Commit" (The history).
  4. Annotated Tags: A commit ID with a personal message attached.

2. Content-Addressable Storage

Git is a "Key-Value" store where the Key is a Hash of the Content. If you have two identical files in different folders, Git only saves One Blob. This is why Git is so incredibly efficient with disk space—it deduplicates your entire project history automatically.


3. The Power of the SHA (Secure Hash Algorithm)

A Git ID (e.g., a1b2c3d4...) is a 40-character string calculated using the content of the object.

  • If even one bit of your code changes, the SHA changes.
  • This provides Integrity. It is mathematically impossible to change your project's history without changing the final Hash. This is why you can trust that your download from GitHub is exactly what the author pushed.

4. Delta Compression and Packfiles

If you save a 1MB file and then change one word, does Git save another 1MB file? Eventually, no. Git performs Garbage Collection (git gc). It finds similar files and creates a "Packfile" where it only stores the Difference (Delta) between them. This turns gigabytes of history into megabytes of highly compressed data.


Frequently Asked Questions

Can I delete the .git folder? Yes, but you will lose your entire project history. Your current files will stay, but they will no longer be "version controlled." NEVER delete this folder if you value your work.

What is the 'HEAD'? HEAD is just a text file (at .git/HEAD) that contains a pointer to the branch you are currently on. If you open it, you'll see: ref: refs/heads/main.


Key Takeaway

Git is not a complex mystery; it's a masterpiece of data-structure design. By understanding Blobs, Trees, and Hashes, you stop "Guessing" what Git is doing and start understanding why it acts the way it does. You have achieved the final level of Git Mastery—knowledge of the internal machine.

Read next: Mastering the Git Workflow: Pro Tips for Seniors →


Part of the GitHub Mastery Course — masters of the machine.