Synopsis
git cat-file (-t | -s | -p | -e) <object>
git cat-file --batch
git cat-file --batch-check
Description
The git cat-file command is a plumbing tool that reads raw Git objects from the object database. It can print an object's type (blob, tree, commit, tag), size, and content. The --batch mode lets scripts feed many object names on stdin and receive structured output, which is far faster than invoking cat-file per object.
In day-to-day use, git cat-file integrates closely with shell aliases, editor plugins, and continuous integration. Power users often add aliases that combine flags they always pass, or wrap the command in scripts that enforce team conventions. Output formatting can be customized via Git config — pretty formats, color schemes, and pager behavior are all tunable. When something goes wrong, the first diagnostic step is usually to re-run the command with GIT_TRACE=1 in the environment, which reveals the underlying plumbing calls. For unusual situations, the --help output (git cat-file --help) opens the full manual page with details on every option, including those rarely used in casual workflows but essential for debugging or scripting at scale.
Understanding how git cat-file interacts with the rest of Git's data model — the object database, the index, refs, and the working tree — pays dividends. Each command operates on some subset of these pieces, and knowing which it touches helps predict outcomes and recover from mistakes. Reading the official Git documentation alongside hands-on practice in a throwaway repository is the fastest way to internalize the nuances. Most production issues with Git stem from one of three causes: surprising default behavior, partial network operations, or rewriting history that was already shared. A working mental model of git cat-file's side effects helps avoid all three.
When to Use
Most developers never need git cat-file directly — porcelain commands like git show, git log, and git diff wrap it transparently. Reach for it when writing scripts that walk Git objects, debugging repository corruption, or learning how Git stores data internally.
Common Options
| Option | Description |
|---|---|
-t | Print object type. |
-s | Print object size in bytes. |
-p | Pretty-print contents in a type-aware way. |
-e | Exit zero if the object exists. |
--batch | Stream <sha> <type> <size> CRLF <content> for each input. |
--batch-check | Like batch, but only header lines. |
--filters | Apply smudge filters when printing blobs. |
Examples
git cat-file -t HEAD
# commit
git cat-file -p HEAD
# Show commit message and tree/parent SHAs
git cat-file -p HEAD:src/main.c
# Print the file as it was at HEAD
echo HEAD | git cat-file --batch-check
# Get type/size/SHA in machine-readable form
Common Mistakes
Passing a partial SHA that's ambiguous returns an error — disambiguate with more characters. Forgetting -p and using -t doesn't print contents. --batch mode requires careful protocol handling; respect the size header to read the right number of bytes.
Related Commands
git show, git hash-object, git ls-tree, git rev-parse