Govur University Logo
--> --> --> -->
...

In which specific subdirectory within the `.git` folder are all of Git's internal objects (blobs, trees, commits) stored?



Git's internal objects, including blobs, trees, and commits, are stored in the specific subdirectory located at `.git/objects`. This directory serves as the repository's object database, containing all the fundamental data that comprises the project's history and content. Each object within this database is uniquely identified by a 40-character SHA-1 hash, which is a cryptographic checksum of its content and type. This design principle makes Git a content-addressable filesystem, meaning you retrieve data based on its content's hash rather than its name or location. The integrity of the data is also ensured by this hashing.

There are three primary types of objects stored here: a blob object stores the exact content of a file. It does not contain any filename or path information; it is purely the data. If multiple files in the repository have identical content, Git stores only one blob object for that content. A tree object represents a directory and maps filenames and file modes to either blob objects (for files) or other tree objects (for subdirectories). It essentially captures a snapshot of a directory's structure and contents at a given point in time. A commit object represents a specific version or snapshot of the entire project's working directory. It contains a pointer to the top-level tree object for that snapshot, one or more pointers to its parent commit objects (defining the project's history), the author's and committer's name and email, timestamps, and a commit message.

Within the `.git/objects` directory, these objects are stored in two main formats. Initially, when objects are created, they are typically stored as 'loose objects'. These are compressed files located in subdirectories whose names are the first two hexadecimal characters of the object's SHA-1 hash. The remaining 38 characters of the hash form the object's filename. For instance, an object with the SHA-1 hash `da39a3ee5e6b4b0d3255bfef95601890afd80709` would be found at `.git/objects/da/39a3ee5e6b4b0d32555bfef95601890afd80709`.

To optimize storage and network transfer for repositories with a large number of objects, Git periodically consolidates these loose objects into 'packfiles'. These highly compressed files are stored in the `.git/objects/pack` subdirectory. A packfile, identifiable by a `.pack` extension, stores multiple objects together, often using delta compression where only the differences between similar objects are stored, significantly reducing disk space. Accompanying each `.pack` file is an `.idx` index file, which provides an efficient lookup table for quickly locating specific objects within the packfile without needing to decompress or scan the entire file. This combined approach ensures both flexibility for rapidly adding new objects and efficient long-term storage.