Git Internals

Back

Loading concept...

🏛️ Git Internals: The Secret Library Behind the Curtain

The Magical Analogy: Imagine Git is a magical library where every book, every page, every letter is stored in special treasure chests. Each chest has a unique lock code that never changes. Let’s explore this magical place!


🎭 The Story Begins

You’ve been using Git like a wizard casting spells: git add, git commit, git push. But have you ever wondered what happens behind the curtain?

Today, we’re going to sneak into the secret basement of Git’s magical library and see how the magic really works.


📦 Git Object Types: The Four Treasure Chests

In Git’s basement, there are exactly four types of treasure chests. Each holds different things.

🌳 The Four Objects

graph TD A["🗃️ Git Objects"] --> B["📄 Blob"] A --> C["📁 Tree"] A --> D["📸 Commit"] A --> E["🏷️ Tag"] B --> B1["Stores file content"] C --> C1["Stores folder structure"] D --> D1["Stores a snapshot"] E --> E1["Stores a named marker"]

📄 Blob (Binary Large Object)

Think of a blob like a page torn from a book. It holds the actual content of a file—but it doesn’t know its own name!

Example: Your hello.txt file with “Hello World” inside becomes a blob storing just “Hello World”.

📁 Tree

A tree is like a table of contents. It says “Chapter 1 is on page 5, Chapter 2 is on page 12…”

Trees connect file names to their blobs. They also point to other trees (subfolders!).

📸 Commit

A commit is like a photograph of your entire project at one moment. It says:

  • “Here’s the tree (snapshot)”
  • “Here’s who took the photo”
  • “Here’s when it was taken”
  • “Here’s the previous photo”

🏷️ Tag

A tag is like putting a sticky note on a specific photo saying “THIS IS VERSION 1.0!”


🔐 SHA-1 Hashes: The Magical Lock Codes

Every treasure chest in Git has a 40-character lock code called a SHA-1 hash.

What Makes It Special?

e83c5163316f89bfbde7d9ab23ca2e25604af290

This weird string is like a fingerprint. Here’s the magic:

  1. Same content = Same fingerprint — If two files have identical content, they get identical codes
  2. Any tiny change = Completely different code — Change one letter, and the whole code changes
  3. One-way magic — You can turn content into a code, but never turn a code back into content

🎯 Simple Example

# Git turns "Hello" into this hash:
echo "Hello" | git hash-object --stdin
# Result: ce013625030ba8dba906f756967f9e9ca394464a

# "Hello!" (with exclamation) becomes:
echo "Hello!" | git hash-object --stdin
# Result: 69ae8d8bd13fa07e63899f9c47edf09cec0a4c82

💡 Why 40 characters? SHA-1 creates a 160-bit number. Written in hexadecimal (0-9, a-f), that’s exactly 40 characters. That’s over 1 trillion trillion trillion possible combinations!


🧭 Refs and HEAD: The Magical Bookmarks

What Are Refs?

Refs are bookmarks pointing to commits. Instead of remembering e83c5163316f89bfbde7d9ab23ca2e25604af290, you just say “main” or “feature-login”.

graph LR A["refs/heads/main"] --> B["abc123..."] C["refs/heads/feature"] --> D["def456..."] E["refs/tags/v1.0"] --> B F["HEAD"] --> A

Where Do Refs Live?

Inside your .git folder:

.git/
├── HEAD                 ← The special "you are here" marker
├── refs/
│   ├── heads/
│   │   ├── main        ← Branch bookmark
│   │   └── feature     ← Another branch
│   └── tags/
│       └── v1.0        ← Tag bookmark

👆 The HEAD File

HEAD is your “You Are Here” sign on the library map.

Normal state (attached to a branch):

ref: refs/heads/main

This says: “I’m currently on the ‘main’ branch”

Detached state (pointing directly to a commit):

e83c5163316f89bfbde7d9ab23ca2e25604af290

This says: “I’m looking at this specific commit, not following any branch”


🔍 Inspecting Git Objects: X-Ray Vision

Git gives you special glasses to peek inside any treasure chest!

git cat-file — The X-Ray Tool

# See what TYPE of object it is
git cat-file -t abc123
# Output: commit (or blob, tree, tag)

# See the SIZE of the object
git cat-file -s abc123
# Output: 243 (bytes)

# See the actual CONTENT
git cat-file -p abc123
# Output: (shows the contents)

🎯 Real Example: Inspect a Commit

git cat-file -p HEAD

Output:

tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904
parent 9e5e6a4c6f3f0e8e9f0a1b2c3d4e5f6a7b8c9d0e
author Alice <alice@example.com> 1699999999 +0000
committer Alice <alice@example.com> 1699999999 +0000

Add login feature

This tells you:

  • 📁 tree — The snapshot of files at this commit
  • ⬅️ parent — The previous commit
  • 👤 author — Who wrote the changes
  • 💬 message — What they wrote about it

Inspect a Tree

git cat-file -p 4b825dc642cb

Output:

100644 blob a1b2c3d4...  README.md
100644 blob e5f6a7b8...  index.js
040000 tree 9c0d1e2f...  src
  • 100644 = regular file
  • 040000 = directory (another tree!)

😵 Detached HEAD State: When You’re Lost in Time

What Is It?

Normally, HEAD points to a branch name, which points to a commit:

graph LR HEAD --> main --> C3["Commit 3"] C3 --> C2["Commit 2"] C2 --> C1["Commit 1"]

In detached HEAD, you point directly to a commit, bypassing the branch:

graph LR HEAD --> C2["Commit 2"] main --> C3["Commit 3"] C3 --> C2 C2 --> C1["Commit 1"]

How Does It Happen?

# Checkout a specific commit
git checkout abc123

# Checkout a tag
git checkout v1.0

# Checkout a remote branch without tracking
git checkout origin/main

Git will warn you:

You are in 'detached HEAD' state...

⚠️ The Danger

In detached HEAD, if you make commits, they’re orphaned — not attached to any branch. If you switch away, those commits can be lost forever!

🛟 How to Escape

Option 1: Go back to a branch

git checkout main

Option 2: Create a new branch to save your work

git checkout -b my-new-branch

Option 3: Already made commits? Save them!

# While still detached, create a branch
git branch rescue-branch
git checkout rescue-branch

🎯 Putting It All Together

graph TD subgraph "What You See" A["Your Files"] end subgraph "Git's Secret Basement" B["Blobs"] --> |"content"| A C["Trees"] --> |"structure"| B D["Commits"] --> |"snapshot"| C E["Refs"] --> |"bookmark"| D F["HEAD"] --> |"you are here"| E end G["SHA-1 Hash"] --> |"unique ID for"| B G --> |"unique ID for"| C G --> |"unique ID for"| D

🌟 Key Takeaways

Concept Remember It As
Blob Page content (no name)
Tree Table of contents
Commit Snapshot photo
Tag Sticky note label
SHA-1 40-char fingerprint
Refs Bookmarks to commits
HEAD “You are here” marker
Detached HEAD Lost in time travel

🚀 You Did It!

You’ve just peeked behind Git’s magical curtain! Now when you use Git commands, you’ll know there are tiny blobs and trees being created, hashes being calculated, and refs being updated — all working together like clockwork in the secret basement.

Next time someone mentions “Git internals,” you can smile knowingly. You’ve been to the basement. You’ve seen the treasure chests. You understand the magic.

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.