Lesson Completion
Back to course

git cat-file

Intermediate
7 minutes4.7Git

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:

FlagWhat It ShowsExample
-tObject typecommit, tree, blob, tag
-sObject size (bytes)234
-pPretty-printed contentFull object data

What -p shows for each type:

Object Type-p Output
BlobRaw file content
TreeDirectory entries: mode, type, SHA, name
CommitTree SHA, parents, author, committer, message
TagTagged 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

flowchart LR SHA["🔑 SHA: abc123"] --> CATFILE["git cat-file"] CATFILE -->|"-t"| TYPE["Type: commit"] CATFILE -->|"-s"| SIZE["Size: 234 bytes"] CATFILE -->|"-p"| CONTENT["Content: full object"] style SHA fill:#1a1a2e,stroke:#ffd700,color:#ffd700 style CATFILE fill:#0f3460,stroke:#53d8fb,color:#53d8fb

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

bash
# ─── 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.0

Key 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:filename to access a blob.

Interview Prep

  • Q: What does git cat-file -p HEAD show? 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.js shows the content of app.js as 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 is commit, it's lightweight (pointing directly to a commit). If it's tag, it's annotated (a separate tag object).

Topics Covered

Git InternalsPlumbing

Tags

#git#internals#cat-file#plumbing

Last Updated

2026-02-13