The Hook (The "Byte-Sized" Intro)
git cat-file is your X-ray vision into Git's database. Give it any SHA, and it tells you: what type of object it is, how big it is, and what's inside. It's the single most important plumbing command for understanding Git internals — and the first tool to reach for when you need to inspect anything at the object level.
📖 What is git cat-file?
git cat-file reads Git objects by their SHA and displays their type, size, or content. It works on all four object types: blobs, trees, commits, and tags.
Conceptual Clarity
The three essential flags:
| Flag | What It Shows | Example |
|---|---|---|
-t | Object type | commit, tree, blob, tag |
-s | Object size (bytes) | 234 |
-p | Pretty-printed content | Full object data |
What -p shows for each type:
| Object Type | -p Output |
|---|---|
| Blob | Raw file content |
| Tree | Directory entries: mode, type, SHA, name |
| Commit | Tree SHA, parents, author, committer, message |
| Tag | Tagged object, type, tag name, tagger, message |
Real-Life Analogy
git cat-file is like opening a sealed envelope. -t reads the label on the outside ("letter," "invoice," "form"). -s reads the weight. -p opens it and reads the full contents.
Visual Architecture
Why It Matters
- Primary inspection tool: The go-to command for examining any Git object.
- Debugging: Understand what any SHA actually contains.
- Learning: See exactly what a commit, tree, or blob looks like inside.
- Scripting: Stable output format for automated processing.
Code
# ─── Check object type ───
git cat-file -t HEAD # commit
git cat-file -t HEAD^{tree} # tree
git cat-file -t HEAD:file.js # blob
# ─── Check object size ───
git cat-file -s HEAD # 234 (bytes)
# ─── View commit contents ───
git cat-file -p HEAD
# tree 4b825dc642cb6eb9a060e54bf899d15643e46ca
# parent a1b2c3d4e5f6789...
# author Jane Doe <jane@example.com> 1707840000 +0530
# committer Jane Doe <jane@example.com> 1707840000 +0530
#
# Add login feature
# ─── View tree contents ───
git cat-file -p HEAD^{tree}
# 100644 blob abc123 README.md
# 040000 tree def456 src
# ─── View blob contents ───
git cat-file -p HEAD:README.md
# # My Project
# Welcome!
# ─── View annotated tag ───
git cat-file -p v1.0.0
# object abc123...
# type commit
# tag v1.0.0
# tagger Jane Doe <jane@example.com>
# Release v1.0.0Key Takeaways
git cat-file -t→ type,-s→ size,-p→ content.- Works on all four object types (blob, tree, commit, tag).
- The most important plumbing command for understanding Git internals.
- Use
HEAD^{tree}to access the tree,HEAD:filenameto access a blob.
Interview Prep
-
Q: What does
git cat-file -p HEADshow? A: It pretty-prints the commit object at HEAD: the root tree SHA, parent commit SHA(s), author/committer info with timestamps, and the commit message. -
Q: How do you view the contents of a specific file at a specific commit? A:
git cat-file -p <commit>:<path>. For example,git cat-file -p HEAD~3:src/app.jsshows the content ofapp.jsas it was 3 commits ago. -
Q: How would you determine if a tag is lightweight or annotated? A: Run
git cat-file -t <tag>. If the result iscommit, it's lightweight (pointing directly to a commit). If it'stag, it's annotated (a separate tag object).