Skip to content
Foxipa

Suggest an edit

Tell us what should be corrected, clarified, or improved. Submission will be connected to Foxipa’s contribution service later.

No information is sent anywhere yet. Your suggestion is only prepared in your browser.

Learning

Git cat-file: Inspect Repository Objects Precisely

Inspect Git objects by type, size, existence, and content, then use cat-file batch modes for efficient scripted object inspection.

Levels: Advanced ~30 min read

Advanced

A Git object has a type, size, and content

`git cat-file` lets you inspect the object named by a revision or object ID without treating the object as a working-tree file. The four primary object types are commit, tree, blob, and tag. The object name identifies the object; `cat-file` tells you what that object is and, when requested, exposes its stored content.

bash
git cat-file -t HEAD
git cat-file -s HEAD
git cat-file -p HEAD

Quick check

What does `git cat-file -t HEAD` report?

Check whether an object exists and is valid

`-e` is useful when a script needs an existence check without printing object contents. It exits successfully when the name resolves to a valid object and nonzero otherwise. This is deliberately different from `git fsck`, which performs broader repository integrity and connectivity checks.

bash
git cat-file -e HEAD
git cat-file -e HEAD^{tree}

Quick check

Which command is the focused existence check?

Read structured objects with pretty printing

`-p` pretty-prints an object according to its type. For a commit, that exposes fields such as its tree, parents, author, committer, and message. For a tree, it presents entries in a human-readable form. This is inspection of the stored object, not a diff against another commit.

bash
git cat-file -p HEAD
git cat-file -p HEAD^{tree}
git cat-file -p HEAD:a-file

Quick check

What is the key distinction between `git cat-file -p HEAD` and `git diff HEAD`?

Inspect a specific object type or raw content

The `<type> <object>` form asks Git to return the object contents as that type, where the requested type must be the real type or a type that can be trivially dereferenced. This is useful when you know that a commit contains a tree and want the tree object itself. Raw object contents are not the same thing as pretty-printed output.

bash
git cat-file commit HEAD
git cat-file tree HEAD
git cat-file blob HEAD:a-file

Quick check

Why might `git cat-file tree HEAD` succeed even though HEAD names a commit?

Use batch-check for efficient metadata inspection

When a script needs metadata for many objects, repeatedly launching `git cat-file` is unnecessary. `--batch-check` reads object names from standard input and reports each object’s name, type, and size by default. A custom format can select fields such as `%(objectname)`, `%(objecttype)`, and `%(objectsize)`.

bash
printf '%s\n' HEAD HEAD^{tree} HEAD:a-file | git cat-file --batch-check
printf '%s\n' HEAD HEAD^{tree} HEAD:a-file | git cat-file --batch-check='%(objectname) %(objecttype) %(objectsize)'

Quick check

Why use `--batch-check`?

Use batch mode when you need object contents too

`--batch` extends the batch interface to return metadata followed by the requested object contents. This is useful for programs that need to stream several objects through one process. The output size is explicit, so a parser can distinguish the metadata line from the exact number of content bytes.

bash
printf '%s\n' HEAD HEAD:a-file | git cat-file --batch

Quick check

What additional data does `--batch` provide compared with `--batch-check`?

Inspect all objects when a repository-wide scan is required

`--batch-all-objects` changes the input model: instead of receiving a list of object names from standard input, `cat-file` visits all objects in the repository and alternate object stores. Combined with `--batch-check`, it is useful for inventory-style scripts. By default the objects are visited in hash order; `--unordered` permits an order chosen for potentially more efficient object access.

bash
git cat-file --batch-check --batch-all-objects
git cat-file --batch-check --batch-all-objects --unordered

Quick check

What does `--batch-all-objects` change?

Practice what you learned

Use the dedicated Git code-completion exercise for this lesson.

Was this page helpful?

Mark this learning resource complete to track your learning progress.