my-notes

git_objects

1 min read

git objects

Understanding Git's internal data structures.


🧱 Object Types

Type Description
blob File contents
tree Directory listing
commit Snapshot + metadata
tag Named reference

πŸ“Š Object Relationships

graph TD
    A[Commit] -->|tree| B[Tree]
    B -->|blob| C[file.txt]
    B -->|tree| D[src/]
    D -->|blob| E[app.js]
    A -->|parent| F[Previous Commit]

πŸ” Examine Objects

View Object Type

git cat-file -t abc1234

Shows type: blob, tree, commit, or tag.


View Object Content

git cat-file -p abc1234

Displays the content of the object.


View Object Size

git cat-file -s abc1234

Shows size in bytes.


πŸ“¦ Blob Objects

Create Blob (Hash a File)

echo "Hello" | git hash-object --stdin

Computes SHA-1 hash of content.


Store Blob

echo "Hello" | git hash-object -w --stdin

Computes hash AND stores blob.


View Blob Content

git cat-file -p abc123

Displays file contents.


🌳 Tree Objects

View Tree

git cat-file -p HEAD^{tree}

Shows directory contents of HEAD.

Output:

100644 blob abc123  README.md
040000 tree def456  src

View Subtree

git ls-tree HEAD src/

Lists contents of src/ directory.


πŸ“ Commit Objects

View Commit

git cat-file -p HEAD

Shows commit details.

Output:

tree abc123...
parent def456...
author Name <email> timestamp
committer Name <email> timestamp

Commit message

Show Commit Tree

git show HEAD --format="%T"

Shows tree hash of commit.


🏷️ Tag Objects

View Tag

git cat-file -p v1.0.0

Shows tag details (for annotated tags).


πŸ“Š Object Storage

Objects stored in .git/objects/:

.git/objects/
β”œβ”€β”€ ab/
β”‚   └── cd1234...   # First 2 chars = folder
β”œβ”€β”€ pack/
β”‚   └── pack-*.pack # Compressed objects

πŸ“ˆ Object Commands

Count Objects

git count-objects -v

Shows object count and size.


Verify Objects

git fsck

Checks object integrity.


Garbage Collect

git gc

Cleans up and optimizes object storage.


πŸ’‘ Tips

Explore Any Object
git cat-file -p main:src/app.js

View specific file at specific branch.

Objects are Immutable

Once created, objects never change - new versions get new hashes.



#git #objects #internals #blob #tree

Built with mdgarden