git cat-file is the primary tool for inspecting the contents of
Git objects from the inside. Use it in scripts, during debugging, and
when learning how Git stores data.
Basic flags
git cat-file -t <sha> # type: blob | tree | commit | tag
git cat-file -s <sha> # content size in bytes
git cat-file -p <sha> # content (pretty-print)
git cat-file -e <sha> # existence check via exit code
Pretty-print adapts its formatting to the object type:
- blob: raw bytes as-is.
- tree: a table of "mode type sha name".
- commit: text representation with tree, parent, author, committer, message.
- tag: annotated tag with all fields.
Examples
What object is under this SHA?
git cat-file -t 8d0e41
# blob
Show the content of the latest commit:
git cat-file -p HEAD
# tree 7e3f9a2b1c4d5e6f...
# parent a1b2c3d4...
# author ...
# committer ...
#
# Commit message
Look inside the root tree of that commit:
git cat-file -p HEAD^{tree}# 100644 blob 5f7e9c12... README.md
# 100644 blob 8a3f2e91... index.html
Check whether an object exists without any output:
if git cat-file -e <sha> 2>/dev/null; then
echo "object is present"
fi
Batch mode
For scripts that read many objects at once:
echo HEAD | git cat-file --batch
# <sha> commit 245
# tree ...
# parent ...
# ...
--batch reads SHAs from stdin one per line and outputs the metadata
plus content for each. On large repositories this is tens of times
faster than calling cat-file -p for each SHA separately.
Pitfalls
- Pretty-print for a blob outputs raw bytes as-is. If the blob is
binary (an image, an archive), this will corrupt your terminal.
Check the type with
-tfirst. - An object may be in a packfile, and
cat-filewill find it regardless. This is the plumbing level; packfiles are transparent. -p HEADshows the commit object, not the tree. To see the directory contents, useHEAD^{tree}orgit ls-tree HEAD.